I have a DateTime variable dt in trigger.Insert of a record which debug result is :

    USER_DEBUG [185]|DEBUG|2016-12-08 00:00:00

We can see that it's the start time 12:00 AM of date: Dec 8th 2016.
When i convert dt using DateTime.newInstance

    DateTime.newInstance(dt.year(), dt.month(), dt.day(), 0, 0, 0);

The result i get is :

    USER_DEBUG [187]|DEBUG|2016-12-07 08:00:00

My SF org has timezone is

    (GMT-08:00) Pacific Standard Time (America/Los_Angeles)

After convertion, dt is start time for the day before!! : Dec 7th 2016

Any one can help me, please?

  • Are you looking to get the current DateTime of your instances timezone? – TSmith Dec 2 '16 at 11:57
  • @TSmith: Yes, i want to get the start time of date in my local timezone – nlonguit Dec 2 '16 at 12:18

In order to get your current instances timezone you would need to be using System.now().

Returns the current date and time in the GMT time zone.

Returns the current Datetime based on a GMT calendar.

DateTime dt = System.now();
// Result will be current DateTime of GMT time zone

Datetime formattedDT = Datetime.newInstance(dt.year(), dt.month(), dt.day(), 0, 0, 0);
// Result will be current DateTime of GMT time zone but formatted to 00:00:00

If you feel you wish to know more about how DateTimes and Date compare, take a look at System vs Date/DateTime class

| improve this answer | |

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.