1

Here's my code:

//return start datetime of current quarter
public static String quarterStartString {
    get{
         Datetime dt = [Select StartDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].StartDate;
         return dt.format('MM/dd/yyyy HH:mm:ss','US/Pacific');
    }
}
public static Datetime quarterStart {
    get{
         return [Select StartDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].StartDate;
    }
}
//return end datetime of current quarter
public static String quarterEndString {
    get{
         Datetime dt = [Select EndDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].EndDate;
         return dt.format('MM/dd/yyyy HH:mm:ss','US/Pacific');
    }
}

public static Datetime quarterEnd {
    get{
         return [Select EndDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].EndDate;
    }
}


System.debug('CE_Utilities.quarterStartString: '+CE_Utilities.quarterStartString);
System.debug('CE_Utilities.quarterEndString: '+CE_Utilities.quarterEndString);
System.debug('CE_Utilities.quarterStart: '+CE_Utilities.quarterStart);
System.debug('CE_Utilities.quarterEnd: '+CE_Utilities.quarterEnd);

It gives me result:

USER_DEBUG|[1]|DEBUG|CE_Utilities.quarterStartString: 06/30/2015 17:00:00

USER_DEBUG|[2]|DEBUG|CE_Utilities.quarterEndString: 09/29/2015 17:00:00

USER_DEBUG|[3]|DEBUG|CE_Utilities.quarterStart: 2015-07-01 00:00:00

USER_DEBUG|[4]|DEBUG|CE_Utilities.quarterEnd: 2015-09-30 00:00:00

2
  • 1
    It looks like the timezone on your user record is 7 hours ahead of US Pacific Time. So when you query the dates, it returns the correct date, but while using the format function, you are specifying US Pacific as the timezone. In this case, the system will adjust the time as per the difference between your timezone and the Pacific timezone. If you change the timezone on your user record, you should get the same result in the logs.
    – Aayush K
    Commented Jul 28, 2015 at 19:32
  • 6
    Use formatGMT instead, you'll get the same output.
    – sfdcfox
    Commented Jul 28, 2015 at 19:42

1 Answer 1

1

I don't know how to mark the comment as answer but sfdcfox's comment works

I also found that if I change 'dt.format('MM/dd/yyyy HH:mm:ss','US/Pacific')' to dt.format('MM/dd/yyyy HH:mm:ss','(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)'); The output is also good.

You must log in to answer this question.

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