-2
String formatedDt = dt.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

I want to used this string in dyamic SOQL but it gives me formatted string in localtimezone, can anyone help me how do i get it in GTM ?

12

Use DateTime's formatGMT to format the time according to your specs in GMT.

String formatedDt = dt.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
2
  • You are a star man !
    – Parth Naik
    Aug 2 '17 at 8:14
  • @sfdxfox your answers from the past are still helpful :) Thanks
    – Abhijith E
    Apr 16 '20 at 17:06
1

you can use the dateGMT() method:

dateGMT()

Return the Date component of a Datetime in the GMT time zone. Signature

public Date dateGMT()

Return Value

Type: Date

Example:

DateTime myDateTime = DateTime.newInstance(2006, 3, 16, 23, 0, 0);
Date myDate = myDateTime.dateGMT();
Date expected = Date.newInstance(2006, 3, 17);
System.assertEquals(expected, myDate);

from the Apex developer Guide

2
  • Thanks for the quick reply.but i need at a string to used as dynamic soql.
    – Parth Naik
    Aug 1 '17 at 14:38
  • see @sfdcfox's answer above - formatGMT method instead of dateGMT() for formatting existing dateTime
    – glls
    Aug 1 '17 at 14:45

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.