0

I am providing a default value to a DateTime field in my apex class.

customobject.DateTime__c = DateTime.parse('01/01/1990 12:00 AM');

I want to set the default value to 01/01/1990 12:00 am UTC time. Am i setting it properly.

My Salesforce organization timezone is set to Pacific. So when I go back and check the field in Salesforce, it is displayed as 01/01/1990 12:00 AM. I am trying to figure out, if it saved the datetime field to 01/01/1990 12:00 AM Pacific time or UTC?

1 Answer 1

2

DateTime.parse() uses the current user's locale and time zone. You can tell because when you view it in the UI, it has the same value you set. It's converted internally to UTC, but the value will not be 01/01/1990 12:00 am UTC because it's adjusted from your local Pacific time.

Instead, use DateTime.newInstanceGmt(year, month, date, hour, minute, second):

DateTime dt = DateTime.newInstanceGmt(1990, 1, 1, 0, 0, 0);
5
  • My apologies for the earlier comment (my kids got a hold of the keyboard!). I think the confusion for the OP stems from the quote "The returned date is in the GMT time zone." but elsewhere says "Constructs a Datetime from the given String in the local time zone and in the format of the user locale." I'm pretty sure a bug needs to be logged.
    – sfdcfox
    Jun 3, 2019 at 18:08
  • ok so DateTime.parse() is going to see the user locale time calling the apex class and not the default time zone setup for the org in Salesforce?
    – Student
    Jun 3, 2019 at 19:28
  • @Student Correct.
    – David Reed
    Jun 3, 2019 at 19:35
  • ok so if I want to set the UTC 12:00. how about I provide customobject.DateTime__c = DateTime.parse('01/01/1990 06:00 PM'); since user might be using Mountain Time and 6:00 PM is equal to 12:00 AM UTC.
    – Student
    Jun 3, 2019 at 19:55
  • Your user might not have the locale you expect. Better to provide a value in GMT if a result in GMT is what you want.
    – David Reed
    Jun 3, 2019 at 20:16

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.