+ Start a Discussion

Format to Insert a value to a Date/Time Field

Hi All,


I was having a hard time figuring out what is the format that the Date/Time field should acccept. The format I am generating to another source code is 5/17/2011 11:00 or dd/mm/yyyy hh:mm. and Salesforce does not accept when I tried to insert. I also tried using the datetime.valueOf but it still does not work, it says invalid date.


Please advise.




This may be a silly question but how should we properly format a datetime value for a datetime field for the current date and time?




customObject.Date_Time_Field__c = datetime.now();




Is that formatted correctly?


You'll be wanting "system.now()".


Thank you for the quick response. I have a quick question...


If I execute anonymous:


system.debug('System Time: ' + system.now());

system.debug('Datetime Time: ' + datetime.now());


I get:


09:43:06.048 (48402000)|USER_DEBUG|[1]|DEBUG|System Time: 2012-10-03 13:43:06

09:43:06.048 (48458000)|USER_DEBUG|[2]|DEBUG|Datetime Time: 2012-10-03 13:43:06


What is the difference between system.now() and datetime.now()?


I'm just wondering. The issue I have is some of my dates are being set in the future for some reason.. like 2016-11-02 11:32:05. Not consistenly but enough to make me wonder what is going on.


Huh, what do you know? According to the docs, they are identical. Both return the current datetime based on GMT.




Hmm... that's pretty interesting.


Thank you for your help.

Jason Fry 5Jason Fry 5
This is from a DataLoader error message:
Valid date format: MM/DD/YYYYThh:mm:ss.mmmTimeZone
(example: 02/20/1987T12:04:12.000GMT+01:00)