last day (15 days later) » 

5:28 AM
0
A: Datetime conversion from GMT to CST

Mohith ShrivastavaYou can use Timezone method for this // Get the CST time zone Timezone tz = Timezone.getTimeZone('America/Chicago'); integer offset = tz.getOffset(System.now()); system.debug(tz.getOffset(System.now())); //offset value in milliseconds Datetime cstDatetime = System.now().addSeconds(offset/1000)...

 
good point . Let me add that as well
 
@Mohit - User timezone is IST , when i executed the above logic it is returning me the IST timezone not the CST timezone. Below is the debug. cstDatetime2019-01-08 10:38:42 Current time in CST is 11:10pm
 
Execute the first one where I have overriden explicitly to CST
 
I can execute the first, but as per the dynamic logic it seems second one is feasible, but it is returning User time zone, so can you please how to change the offset value to be in cst.
 
You will just have to flip the users timezone in user record to America/Chicago instead .
 
5:28 AM
One more small doubt. I am debugging system.debug('cstDatetime' + cstDatetime.hour()); system.debug('cstDatetime' + cstDatetime.minute()); for hour it is returning 15 eventhough time is showing as cstDatetime2019-01-07 23:23:41. Am i missing something here.
hi Mohit
 
Hey when you say hour() it will convert further to local timezone
 
for the above logic i am checking for hour(), it is returning me 15 instead of 23
oh.. then for hour how can i get the cst hour
 
Do not convert to cst then
 
whereas minute is returning correctly
 
Just do system.now().hour()
It will return fine
When you do system.now() debug logs always show in gmt
 
5:31 AM
system.now().hour() - in this case it will return user's locale time zone hour right.. but i want cst hour
 
Yes make sure your users timezone is cst
If you still want cst hour
Then convert time to gmt
And then say hourGM
hourGMT()
 
i mean the user's tmezone has already been set, now we can't change as per the business rule.. so i need to convert it in CST
i can get myDateTime.hourGMT(), but after this how to convert to CST
 
5:46 AM
Subtract offset value
Offset is in millisecond
Convert to the hour and subtract or add
 

last day (15 days later) »