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
sonamsonam 

Display date in User's timezone in a given Format

Please help

 

1. Initially 5hours were added to the GMT.

   But , when i give a future date ...... i get the current date.

 

 

 

public static String printDate(Datetime Value,String format)
  {
  
		datetime systemDate = datetime.now();
		String timeZone = UserInfo.getTimeZone().getID();
		System.debug('getTimeZone-----------------'+timeZone);
		if(!systemDate.isSameDay(Value))
		{
			System.debug('not current date');
			Value = Value.addHours(5);
		}
		
		return Value.format(format);
  
  }

 

2. I tried to pass the timezone but I am unable to correct it.

    It gives me the previous date by the following code.

 

public static String printDate(Datetime Value,String format)
  {
  
	
	datetime systemDate = datetime.now();
	String timeZone = UserInfo.getTimeZone().getID();
	System.debug('getTimeZone-----------------'+timeZone);
	if(!systemDate.isSameDay(Value))
	{
		System.debug('date --------'+ Value);
		System.debug('not current date');
		System.debug('getTimeZone-----------------'+timeZone);
		System.debug('value is time zone>>>'+UserInfo.getTimeZone().getDisplayName());
		String ValueLocalDate = Value.format(format,timeZone);
		System.debug('ValueLocalDate-----------------'+ValueLocalDate);        

		return ValueLocalDate ;
	}
	
	return Value.format(format);
  }

I checked the debug logs to find that the timestamp is taken as 2013-01-05 00:00:00 and not the original time

Time is taken as 00:00:00

 

Thanks in advance.

 

Tim BarsottiTim Barsotti

What is the value of your format and value parameters when your method is called?

sonamsonam

1.Value is dateTime and

2.The format is 'MMMMM dd, yyyy'  but its passed along with the date as functionName(Date ,'MMMMM dd, yyyy');

 

Tim BarsottiTim Barsotti

1) By dateTime do you mean dateTime.now()? 

2) Might want to print the Hours with your debug. e.g. YYYY-MM-DDThh:mm:ss+hh:mm