-1
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 ?

2 Answers 2

13

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, 2017 at 8:14
  • @sfdxfox your answers from the past are still helpful :) Thanks
    – Abhijith E
    Apr 16, 2020 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, 2017 at 14:38
  • see @sfdcfox's answer above - formatGMT method instead of dateGMT() for formatting existing dateTime
    – glls
    Aug 1, 2017 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .