7
String day = string.valueOf(system.now().day());
String month = string.valueOf(system.now().month());
String hour = string.valueOf(system.now().hour());
String minute = string.valueOf(system.now().minute());
String second = string.valueOf(system.now().second());
String year = string.valueOf(system.now().year());

String strTime = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second ;

system.debug(strTime);
system.debug(system.now());

output 1: 2015-7-7 14:5:55

output 2: 2015-07-07 08:35:55

================================================

1
  • Try looking at Json.serialize() to see underlying value irrespective of timezone Jul 7, 2015 at 10:26

2 Answers 2

6

DateTime objects have built-in support for the user's timezone. So DateTime.hour:

Returns the hour component of a Datetime in the local time zone of the context user.

DateTime.format also includes the timezone offset but just doing a default DateTime conversion to a string looks like it presents the underlying GMT value.

1

System.now() returns the DateTime in GMT time zone, whereas System.now().hour() is the method of DateTime class, which returns the local time zone of the context user.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .