Skip to main content
New solution that is capable of handling date whether it is in UTC format or ISO8601 format.
Source Link

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

OP mentioned that day and month i always showing 01. 2 points:

  1. datetime string will be needed in double quotes ( my mistake i disregarded that previously)
  2. deserialize method is only working with proper iso format after using double hence we can check if date is in iso format then deserialize it otherwise just get value of datetime using datetime.valueof

String strDate1 = '2017-12-17T11:50:50.000Z'; String strDate2 = '2016-11-16 11:50:55';

Datetime date1 = (DateTime)(strDate1.contains('T')?(Datetime)JSON.deserialize('"' + strDate1 + '"' ,Datetime.class) : DateTime.valueof(strDate1)); Datetime date2 = (Datetime)(strDate2.contains('T')?(Datetime)JSON.deserialize('"' + strDate2 + '"' ,Datetime.class) : DateTime.valueof(strDate2));

System.debug('date 1 = ' + date1); System.debug('date 2 = ' + date2); System.debug('date11 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt())); System.debug('Date 21 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

====================================================================== Updated Solution

String strDate1 = '2020-01-01T11:50:50.000Z';
Datetime strDate1 = (strDate1==null)?null:Datetime.valueOf(strDate1.replace('T',' '));
Datetime date1 = Date.newInstance(strDate1.yearGmt(),strDate1.monthGmt(),strDate1.dayGmt());

====================================================================== And, this solution works perfectly irrespective of whether the date comes in ISO8601 format or UTC format.

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

OP mentioned that day and month i always showing 01. 2 points:

  1. datetime string will be needed in double quotes ( my mistake i disregarded that previously)
  2. deserialize method is only working with proper iso format after using double hence we can check if date is in iso format then deserialize it otherwise just get value of datetime using datetime.valueof

String strDate1 = '2017-12-17T11:50:50.000Z'; String strDate2 = '2016-11-16 11:50:55';

Datetime date1 = (DateTime)(strDate1.contains('T')?(Datetime)JSON.deserialize('"' + strDate1 + '"' ,Datetime.class) : DateTime.valueof(strDate1)); Datetime date2 = (Datetime)(strDate2.contains('T')?(Datetime)JSON.deserialize('"' + strDate2 + '"' ,Datetime.class) : DateTime.valueof(strDate2));

System.debug('date 1 = ' + date1); System.debug('date 2 = ' + date2); System.debug('date11 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt())); System.debug('Date 21 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

OP mentioned that day and month i always showing 01. 2 points:

  1. datetime string will be needed in double quotes ( my mistake i disregarded that previously)
  2. deserialize method is only working with proper iso format after using double hence we can check if date is in iso format then deserialize it otherwise just get value of datetime using datetime.valueof

String strDate1 = '2017-12-17T11:50:50.000Z'; String strDate2 = '2016-11-16 11:50:55';

Datetime date1 = (DateTime)(strDate1.contains('T')?(Datetime)JSON.deserialize('"' + strDate1 + '"' ,Datetime.class) : DateTime.valueof(strDate1)); Datetime date2 = (Datetime)(strDate2.contains('T')?(Datetime)JSON.deserialize('"' + strDate2 + '"' ,Datetime.class) : DateTime.valueof(strDate2));

System.debug('date 1 = ' + date1); System.debug('date 2 = ' + date2); System.debug('date11 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt())); System.debug('Date 21 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

====================================================================== Updated Solution

String strDate1 = '2020-01-01T11:50:50.000Z';
Datetime strDate1 = (strDate1==null)?null:Datetime.valueOf(strDate1.replace('T',' '));
Datetime date1 = Date.newInstance(strDate1.yearGmt(),strDate1.monthGmt(),strDate1.dayGmt());

====================================================================== And, this solution works perfectly irrespective of whether the date comes in ISO8601 format or UTC format.

added 1045 characters in body
Source Link

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

OP mentioned that day and month i always showing 01. 2 points:

  1. datetime string will be needed in double quotes ( my mistake i disregarded that previously)
  2. deserialize method is only working with proper iso format after using double hence we can check if date is in iso format then deserialize it otherwise just get value of datetime using datetime.valueof

String strDate1 = '2017-12-17T11:50:50.000Z'; String strDate2 = '2016-11-16 11:50:55';

Datetime date1 = (DateTime)(strDate1.contains('T')?(Datetime)JSON.deserialize('"' + strDate1 + '"' ,Datetime.class) : DateTime.valueof(strDate1)); Datetime date2 = (Datetime)(strDate2.contains('T')?(Datetime)JSON.deserialize('"' + strDate2 + '"' ,Datetime.class) : DateTime.valueof(strDate2));

System.debug('date 1 = ' + date1); System.debug('date 2 = ' + date2); System.debug('date11 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt())); System.debug('Date 21 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

OP mentioned that day and month i always showing 01. 2 points:

  1. datetime string will be needed in double quotes ( my mistake i disregarded that previously)
  2. deserialize method is only working with proper iso format after using double hence we can check if date is in iso format then deserialize it otherwise just get value of datetime using datetime.valueof

String strDate1 = '2017-12-17T11:50:50.000Z'; String strDate2 = '2016-11-16 11:50:55';

Datetime date1 = (DateTime)(strDate1.contains('T')?(Datetime)JSON.deserialize('"' + strDate1 + '"' ,Datetime.class) : DateTime.valueof(strDate1)); Datetime date2 = (Datetime)(strDate2.contains('T')?(Datetime)JSON.deserialize('"' + strDate2 + '"' ,Datetime.class) : DateTime.valueof(strDate2));

System.debug('date 1 = ' + date1); System.debug('date 2 = ' + date2); System.debug('date11 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt())); System.debug('Date 21 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

added 490 characters in body
Source Link

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

deserializing your datetime (both utc & ISO) in datetime.class should solve the issue. JSON.desrialize('datetime' , Datetime.class);

i have tested both format in anynomous block and it is working fine.

Datetime effectiveDt = (Datetime) JSON.deserialize('1985-01-01 00:00:00.000',Datetime.class);
system.debug(Date.newInstance(effectiveDt.yearGmt(), effectiveDt.monthGmt(), effectiveDt.dayGmt()));


Datetime effectiveDtISO = (Datetime) JSON.deserialize('1985-01-01T00:00:00',Datetime.class);
system.debug(Date.newInstance(effectiveDtISO.yearGmt(), effectiveDtISO.monthGmt(), effectiveDtISO.dayGmt()));

Credits go to this thread which i have referred : Parsing an ISO 8601 timestamp to a DateTime

OP Example modified

String strDate1 = '2020-01-01T11:50:50.000Z';
String strDate2 = '2019-01-01 11:50:55';

Datetime date1 = (Datetime)JSON.deserialize( strDate1 ,Datetime.class);
Datetime date2 = (Datetime)JSON.deserialize( strDate2 ,Datetime.class);

System.debug('Date 1 = ' + Date.newInstance(date1.yearGmt(),date1.monthGmt(),date1.dayGmt()));
System.debug('Date 2 = ' + Date.newInstance(date2.yearGmt(),date2.monthGmt(),date2.dayGmt()));
Source Link
Loading