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
Milan HrdlickaMilan Hrdlicka 

Insertion of datetime

Hi all,

I need to insert datetime using below code:

Opening_Hours__c oh = new Opening_Hours__c(Name='3', Open__c=2016-12-27T08:00:00Z);
insert oh;

I get this error: "unexpected token: "2016-12-27T08:00:00Z"

Can you please advise on what I am doing wrong?
How can I make a distinction in case of "AM" and "PM"?

Open__c is a field of datewtime type. Thanks a lot.
Best Answer chosen by Milan Hrdlicka
Amit Chaudhary 8Amit Chaudhary 8
Please below code
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

Opening_Hours__c oh = new Opening_Hours__c();
oh.Name='3';
oh.Open__c=System.now();
insert oh;

****************
The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

DateTime myDateTime = DateTime.newInstance(2016, 12, 27, 8, 0, 16);

Opening_Hours__c oh = new Opening_Hours__c();
oh.Name='3';
oh.Open__c=myDateTime;
insert oh;

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please below code
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

Opening_Hours__c oh = new Opening_Hours__c();
oh.Name='3';
oh.Open__c=System.now();
insert oh;

****************
The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

DateTime myDateTime = DateTime.newInstance(2016, 12, 27, 8, 0, 16);

Opening_Hours__c oh = new Opening_Hours__c();
oh.Name='3';
oh.Open__c=myDateTime;
insert oh;

Let us know if this will help you
This was selected as the best answer
Milan HrdlickaMilan Hrdlicka
Yes, it does work! Thanks a lot! :)