5

I want to convert the GMT dateTime to locale dateTime as System.now(). But after converting the format when I am trying to convert that string into dateTime again it is giving me the GMT dateTime.

9

Your question is not very clear to me, but my two possible interpretations are you want to format the current Datetime according to your locale, or you want to adjust the System.now() result to account for the local time zone.

If you just want a locale-specific formatted string to represent the current Datetime, use:

String formatted = Datetime.now().format();

If you are trying to get the current Datetime in the local time zone, you can use the following code:

Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime local = now.addSeconds(offset/1000);
  • Or just String formattedLocalTimeString = DateTime.now().format(). – sfdcfox Jan 11 '17 at 4:00
  • @sfdcfox They want it in the local time zone, no? – Adrian Larson Jan 11 '17 at 4:03
  • 1
    DateTime.format() always returns the value as local time. – sfdcfox Jan 11 '17 at 4:04
  • 4
    DateTime.valueOfGMT(String.valueOf(DateTime.now())) is essentially the same as what you're doing by altering the offset. I just wish I knew this two years ago. Ah well. – sfdcfox Jan 11 '17 at 4:09
  • 3
    It does... the annoying part is that DateTime values always debug in GMT. You have to change it to a string to get local values. – sfdcfox Jan 11 '17 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.