function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jereriah ManningJereriah Manning 

Date.day() versus actual Datetime.Today()

I'm trying to return today's date to a String (a lot more going on), but here's the issue in code and log.

CODE
Datetime todayDate = Date.Today();
Integer todayDay = todayDate.Day();
String todayDateString = todayDate.format('dd-MMM-yyyy hh:mm aaa');
finalMergefieldMap.put('{!Today}', todayDateString);
System.debug('****************************** Date.Today() ' + Date.Today());
System.debug('****************************** Day of Today ' + todayDay );
System.debug('****************************** todayDateString ' + todayDateString );

LOG (trimmed for legibility)
11:19:12.112 (112942417)|USER_DEBUG|[285]|DEBUG|****************************** Date.Today() 2014-07-25 00:00:00
11:19:12.113 (113000818)|USER_DEBUG|[286]|DEBUG|****************************** Day of Today 24
11:19:12.113 (113029002)|USER_DEBUG|[287]|DEBUG|****************************** todayDateString 24-Jul-2014 06:00 PM

Where is the switch between 25 and 24 happening? I checked my Company Profile for timezone.  I did the same on my User record and they are both correctly set to -6:00 (Colorado, USA).

Any help here is most welcome. I don't want to stamp the records with yesterday's date.

Thanks!
~J

@Conga_Jereriah
Best Answer chosen by Jereriah Manning
Jereriah ManningJereriah Manning
Thanks.

Working with this, I ended up getting the users timezone with a Timezone call.
TimeZone tz = UserInfo.getTimeZone();
String timezoneName = tz.getDisplayName();
Datetime todayDate = Date.Today();
String todayDateString = todayDate.format('dd-MMM-yyyy', timezoneName);

All Answers

Denis VakulishinDenis Vakulishin
For getting correct string value you should pass Timezone like
format('yyyy-M', 'CDT')
Date.today() returns GMT timezone when toString() is called(in debug),
Date.Day() and Date.format() returns in your timzone by default.

DISCLAIMER:
This is not much proven information. It's my thoughts based on information that I find in web.
Jereriah ManningJereriah Manning
Thanks.

Working with this, I ended up getting the users timezone with a Timezone call.
TimeZone tz = UserInfo.getTimeZone();
String timezoneName = tz.getDisplayName();
Datetime todayDate = Date.Today();
String todayDateString = todayDate.format('dd-MMM-yyyy', timezoneName);
This was selected as the best answer