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
RajanJasujaRajanJasuja 

Is there any way to get current date and time as per the logged in user time zone in apex?

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11">

Hi,


Is there any way to get current date and time as per the logged in user time zone in apex?

I used System.now(), but its returning as per GMT time zone. I need it as per the user time zone.Like if logged in use time zone is Singapore time zone then it should return current time and date  as per the Singapore time zone which is GMT+8,  not as per the GMT time zone. 


Thanks and Regards,

Rajan Jasuja


Best Answer chosen by Admin (Salesforce Developers) 
JPSeaburyJPSeabury
You can manipulate the date using the "format" instance method.  The following Apex method takes the current datetime, and displays it in "long date" format:
 
Code:
public class MaintCalendar {

  String LongDate = ' ';

  // Init cLongDate with the current Datetime in long date format    
  public String getLongDate() {
    Datetime cDT = System.now();
    LongDate = cDT.format('EEEE, MMMM d, yyyy');
    return LongDate;
  }

}

 

The first line in the getlongDate method initializes the ‘cDT’ variable with the current datetime.  The second line uses the “format” instance method to set a global string variable “LongDate”, using the following SimpleDateFormat string: ‘EEEE, MMMM d, YYYY’  The four letters indicate that the Day, Month and Year should be spelled out in full (non-abbreviated) form. 

 

With the Java Class SimpleDateFormat instance method, you can display the date in any format that you want.  Read more about Java Class SimpleDateFormat on the Java website:  http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

 

The code (and the VF page output) is displayed below.

 



Message Edited by JPSeabury on 09-16-2008 06:59 AM

All Answers

JitendraJitendra
Hi Rajan,

Have you tried

Date obj = Date.today();

SaaniaSaania
I am having the same issue... even date.today() does not return values in users time zone (In an apex trigger btw). Is there a way to get just the Date and not the time at all?

Thanks,
JPSeaburyJPSeabury
You can manipulate the date using the "format" instance method.  The following Apex method takes the current datetime, and displays it in "long date" format:
 
Code:
public class MaintCalendar {

  String LongDate = ' ';

  // Init cLongDate with the current Datetime in long date format    
  public String getLongDate() {
    Datetime cDT = System.now();
    LongDate = cDT.format('EEEE, MMMM d, yyyy');
    return LongDate;
  }

}

 

The first line in the getlongDate method initializes the ‘cDT’ variable with the current datetime.  The second line uses the “format” instance method to set a global string variable “LongDate”, using the following SimpleDateFormat string: ‘EEEE, MMMM d, YYYY’  The four letters indicate that the Day, Month and Year should be spelled out in full (non-abbreviated) form. 

 

With the Java Class SimpleDateFormat instance method, you can display the date in any format that you want.  Read more about Java Class SimpleDateFormat on the Java website:  http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

 

The code (and the VF page output) is displayed below.

 



Message Edited by JPSeabury on 09-16-2008 06:59 AM
This was selected as the best answer
SaaniaSaania

thanks JPSeabury, it worked !!
Mohammad AnisMohammad Anis

Hi All,

Please use the below code and it works:

To get the current Datetime in the local time zone, you can use the following code:

Datetime now = Datetime.now(); Integer offset = UserInfo.getTimezone().getOffset(now); Datetime local = now.addSeconds(offset/1000);

If it helps don't forget to mark this as a best answer!!!

Thanks,
Mohammad Anis

Noam Damri 14Noam Damri 14
 thx Mohammad Anis!