I need to set a datetime-local
picker's default value to the current local time. Native JS seems to output in local time by default:
new Date($.now()); // "Sat Nov 12 2016 22:36:52 GMT+1100 (AEDT)"
However functions like toISOString()
output in UTC, and although I can pull out individual components locally, I don't really want to fiddle around with padding and such. So I try this using moment.js:
moment().local().format(); // "2016-11-12T22:34:05+11:00"
Cool! Now I just need to adjust the format to a tiny bit:
moment().local().format('YYYY-MM-DThh:mm'); // "2016-11-12T10:39"
Waaaaaaait. Now it's in UTC again, even though I specified local.
In this particular case I could use string manipulation to just drop the end off for the date-time picker, but surely I'm going to reach a point where I want to output the local time in an arbitrary format. Am I missing something here?
format(); moment(). format(String); This is the most robust display option. It takes a string of tokens and replaces them with their corresponding values.
updateLocale(localeName, config) to change an existing locale. moment. defineLocale(localeName, config) should only be used for creating a new locale.
moment(). format(String); Parameters: This function accept single parameter of string type, which defines the format. Return Value: This function returns the date.
Your second example isn't UTC, it's just using 12h format.
hh
= 12h, HH
= 24h. Try this instead:
moment().local().format('YYYY-MM-DTHH:mm')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With