How to format date in moment.js based on current locale? - javascript

I want to format the date based on the current browser locale. Like navigator.language. This works
moment(new Date()).locale(navigator.language).format("llll");
but do I need to use the .locale(navigator.language)? I want to know if I need that explicitly, or does moment use the current locale by default, so that I would just need
moment(new Date()).format("llll");
Thanks

Related

Dealing with time in different timezones react/javascript (PST/PDT)

So I have this use case where a user should be able to enter data into a react site having an order_fulfilment_datetime_str which is supposed to be a RFC 3339 datetime string according to the specs of the api this would be posted to.
So essentially a user(in any timezone) selects a (future) date and time (using a plugin such as react-datepicker) assuming that the date time is going to be in PST/PDT.
But the plugin and javascript date object returns the selected date time in the browser's timezone. So I have two things to solve:
How do I make sure that selected date/time is in PST/PDT
How to deal with DST?
Currently what I am trying to do is to simply get individual date, hours, minutes using getHours(), getMinutes(), getDate() to form the string. But again then how will I find out if this selected datetime has offset of -8 or -7(i.e. PST/PDT)?
I checked date-fns and date-fns-tz library but was not sure how to solve this issue using these.
You can use luxon library for setting a global timezone that you want, and then whenever a user selects a date-time using a date-time picker you can simply convert that into the timezone you want.
import { Settings } from "luxon"
Settings.defaultZone = <time-zone>
The above code snippet will set a default zone for the app
Now when a user selects a date-time convert it to the timezone you want using the below code
DateTime.fromISO(dateTimeValue, { setZone: true})

moment locale not set correctly

We're trying to find a problem with tests that use moment.js and fail when run on a server in Arizona but succeed when run locally here in the UK. We manually set the locale before creating a moment and can see that the local 'en-gb' is installed.
This fiddle highlights what I think the problem is (Need to set computer to Arizona Time zone first!) https://jsfiddle.net/2ve10ax4/
moment.locale('en-gb');
console.log(moment.locale());
// en-gb
console.log(moment('2016-05-01T00:00:00+00:00').format());
// 2016-04-30T17:00:00-07:00
I'm expecting to see the date formatted with respect to 'en-gb' locale but it shows it with respect to Arizona time. Am I missing something?
Locale and timezone are orthogonal things. A locale does not specify a timezone unambigously. You need to set your timezone separately. You can use Momemt Timezone, and then you can use e.g.
.tz('Europe/London')
By setting moment.locale('en-gb') you have specified a locale/language (brittish-english), not a timezone.
To specify a timezone you need to include the Moment.js Timezone library in your project, and then do something like:
console.log(moment('2016-05-01T00:00:00+00:00').tz('America/Phoenix').format());
The above will give you the timezone for Arizona.
After reading more I believe parseZone is the correct solution, given that I am already including Time Zone information in the date string.
console.log(moment.parseZone('2016-05-01T00:00:00+00:00').format());

JavaScript - formatting date with current timezone

I am using the moment.js library. I have dates in this format: "2014-06-19T18:00:00+02:00" (string). I want to display the date formatted with the user's timezone.
moment.js docs specifies to use moment("2014-06-19T18:00:00+02:00").utc().format()
My questions are:
How can I set the timezone once and not have to put .utc() every time?
How can I test the timezone functionality? Should I change my computer's clock?
Calling .utc() would actually translate the value you put in to UTC. If you want the user's local time zone, then you should just omit that and call .format().
That you passed a +02:00 offset with the original value doesn't matter. By default, moment will adjust the value to the user's time zone.
On the other hand, if you want to keep the offset you provided, regardless of the user's time zone, then you can use the parseZone function.

how to get local date/time format?

I want to get systems local date/time format and display date from database on this same format. e.g if system date format is like Month dd/yyyy and date stored in database is mm/dd/yyyy format then I need to get local date/time format and convert my stored date into local format.
HTML5 input type="date" this takes default systems date/time format and show date in same format. how can I do this with jquery or javascript.
You could use the JavaScript Date object:
var date = new Date(yearFromDb, monthFromDb, dayFromDb);
date.toLocaleDateString();
EDIT
It seems that the above method is not consistent between different browsers (Chrome, IE, Firefox). Therefore, I think your only option is to get the formatted date string from server side.
Here is a jsFiddle that shows a cross-browser way of getting the users locale, if you can use this to get a formatted date string in the current locale from the backend (if any).
var locale = window.navigator.userLanguage || window.navigator.language;

How to check Date Locales for Date Object

In my browser
new Date("05/06/2013")
is treated as M/D/YY. So above date will 5th of June.
I am using the US date format. I believe JavaScript lets the browser decide the locale. But how do I check what locale the browser users at runtime?
Thanks.

Categories

Resources