1

Hi I'm having an issue with getting the non-localized time of a DateTime's time value. Here's some of the execute anonymous I'm having trouble with.

DateTime aDate = DateTime.newInstance(System.now().date().addDays(5), 
System.now().time());
System.debug(aDate);
String strVal = String.valueOf(aDate);
System.debug(strVal);

So this prints out the following:

2019-03-11 14:57:54
2019-03-11 09:57:54

It first shows 9:57 in UTC and adjusted for daylight savings time which is nice. And then when I convert it, it shows it converted back to 9:57 local time which is awesome. But... it converts it without the daylight savings adjustment. So it shows me local time (good) but it also adjusts for today's daylight savings time status.

Basically I can't access the adjusted version of the value or even know that it was changed +/- an hour because it always changes it back before returning it.

0

String.valueOf always returns the GMT version of the DateTime. Instead, use DateTime.format(strFormat) to get the local time version. This method uses the same format as Java's SimpleDateFormat class. For example:

System.debug(aDate.format('hh:mm:ss aa'));

See SimpleDateFormat for various options.

3
  • Well I'm able to get local time just fine. It's just that the local time lacks the the daylight savings +/- hour adjustment. I could do the adjustment myself but every time I try and retrieve the time, it tells me it's already correct because it "fixed" it before giving me the value. – Dylan Mar 6 '19 at 16:37
  • @Dylan DST should already be accounted for. – sfdcfox Mar 6 '19 at 17:18
  • Right but I guess I was hoping you could view the time after creating the DateTime to see how much +/- it was adjusted by. But every time you retrieve it, it just adjusts it back so you never know what the adjustment was. I guess the real answer is you would need to see if the amount of days you're adding goes past a daylight savings time change and plan accordingly. – Dylan Mar 7 '19 at 15:51

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.