3

I have a public community which means that guest users will be sharing the same guest user record (But not all users will be guest). I am trying to create a way to for them to enter a dateTime and send it back to the server. It looks like ui:inputDateTime automatically converts the selected dateTime using the timezone on the user record.

Example 1:

  • User Time Zone (on Salesforce user record) = GMT

  • <ui:inputDateTime>: user enters January 26 12:00 PM

  • value =2017-01-26T12:00:00.000Z

Example 2:

  • User Time Zone (on Salesforce user record) = EST
  • <ui:inputDateTime> user enters January 26 12:00 PM
  • value = 2017-01-26T17:00:00.000Z

As guest users will be sharing the same record , I am trying to use moment.js to convert the time to utc using the utc-offset of the browser.

 var dateTime=component.find("UIdateTimeCmp").get("v.value"); 

 var convertedNewTime = moment(dateTime).utc().format('YYYY-MM-DD HH:mm:ss');

This worked when I tried using lightning:input type="Datetime-local" but then I discovered IE and firefox don't support it so I am switching to ui:inputDateTime.

The "z" makes moment think that the time is already in GMT so there is no conversion. A workaround for this I came up with is that If I know the user is a guest user and I know that the guest user record has a GMT timezone, I found that I could slice the z before converting.

 var dateTime=component.find("UIdateTimeCmp").get("v.value"); 
 dateTime= dateTime.slice(0,-1);
 var convertedNewTime = moment(dateTime).utc().format('YYYY-MM-DD HH:mm:ss');

But then this messes things up for users who are not guest users and do not have the GMT timezone on their record.

//salesforce timezone = EST
//User enters 12:00 PM. (Utc time should be 17:00:00)

var dateTime=component.find("UIdateTimeCmp").get("v.value"); //2017-01-26T17:00:00.000Z
dateTime= dateTime.slice(0,-1); 
var convertedNewTime =  moment(dateTime).utc().format('YYYY-MM-DD HH:mm:ss'); // 2017-01-26T22:00:00

I need a way to get the actual value the user sees in the datepicker rather than the value converted to the user record's timezone, so that I can use moment.js to convert the value to the browser's utc-offset.

  • 1
    if you are able to identify the user is a non-guest user, have a flag something like isGuest as a aura:attribute and skip the timezone conversion logic if its value is false – Praveen Jan 25 '17 at 17:31
3

The Lightning Framework has a localization service that you can use in your JS to format date/time values which should be helpful to you with your issue.

// get currrent date/time and localize based on user's browser settings

var dateFormat = $A.get("$Locale.dateFormat");
var dateString = $A.localizationService.formatDateTime(new Date(), dateFormat);


// explicitly format date/time value

var dateFormat = "MMMM d, yyyy h:mm a";
var userLocaleLang = $A.get("$Locale.LangLocale");
return $A.localizationService.formatDateTime(date, dateFormat,userLocaleLang);

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.