[SalesForce] Lightning LocalizationService datetime

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

Best Answer

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.

Related Topic