5

I'm trying format ui:inputdatetime to multiple timezones, but i'm seeing inconsistent values, is it correct behavior? is it a good idea to rely on localization service on client side?

var format = $A.get("$Locale.timeFormat");
format = format.replace(":ss", "");
var langLocale = $A.get("$Locale.langLocale");
var timezone = $A.get("$Locale.timezone");
var d = new Date();
console.log("time in ===== "+timezone+" is "+d);
var datetimetemp;
$A.localizationService.WallTimeToUTC(d, timezone, function(walltime) {
    datetimetemp = $A.localizationService.formatDateTimeUTC(walltime, format, langLocale);
    console.log("converted time ===== "+datetimetemp);
});

time in ===== America/Los_Angeles is Mon Sep 26 2016 14:15:29 GMT-0700 (Pacific Daylight Time)

converted time ===== 4:15 AM

as per google utc is 9:16 PM at that moment

thanks

4

I've got a better answer finally:

It's because you're using both $A.localizationService.WallTimeToUTC and $A.localizationService.formatDateTimeUTC, which means you're converting the date 2 times. The second time you should be using $A.localizationService.formatDateTime instead.

The code sample from Aura doc should be updated I guess.

2

If you're on a Winter '17 org, you should use the new Lightning Base Components. You can leverage <lightning:formattedDateTime> that will do the formatting for you according to the user preferences.

  • I'm in winter 17 org but not able to save getting error, Failed to save undefined: No COMPONENT named markup://lightning:formattedDate found : [markup://c:TimeZoneCompare]: Source. Also I think its an output type component? i'm looking for user to select one date time field and update other datetime before they save, they can choose either one of the widget to change datetime to compare. – realnumber Sep 27 '16 at 15:19
  • Actually the name of the component is lightning:formattedDateTime and not lightning:formattedDate. It seems to be a typo in the doc samples. But you're right, it seems to work only for output. – Fabien Taillon Sep 28 '16 at 10:47
  • I need to use timeZone="GMT" for dates to output correctly. I am in EST, and the dates show a day early if I use the default timeZone – Scott Pelak Aug 2 '18 at 13:54

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.