Why is it that if I choose a year below 1848, the result of this format is May 10?
I have a feeling this could be about time zones? If so how can I avoid this, given that I will be creating a date object from an ISO date string (without time) like this: YYYY-MM-DD.
(Tested on Chrome 59)
const workingDate = Intl.DateTimeFormat('en-GB').format(new Date('Fri May 11 1848 01:00:00 GMT+0100 (BST)'));
const notWorkingDate = Intl.DateTimeFormat('en-GB').format(new Date('Fri May 11 1847 01:00:00 GMT+0100 (BST)'));
console.log(workingDate);
console.log(notWorkingDate);
Date strings above are from e.g. new Date('1847-05-11') (I'm in BST time zone)
This test was made in Chrome 53.
I've added some options to DateTimeFormat to check the other fields of the date:
options = {
year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric',
hour12: false, timeZoneName: 'long'
};
var workingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1848 01:00:00 GMT+0100 (BST)'));
var notWorkingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1847 01:00:00 GMT+0100 (BST)'));
The result was:
workingDate: 10/05/1848, 20:53:32 GMT-03:06:28
notWorkingDate: 10/05/1847, 20:53:32 GMT-03:06:28
Most places didn't have standardized UTC-based offsets before 1900 (actually, each country adopted it in a different year), so before 1900 you always get those strange results. Actually, as Matt explained in the comments, UTC was implemented in 1972 and before that most zones were defined as offsets from GMT. Anyway, for very ancient dates, specially before 1900, you might expect offsets like the above.
In this case, it's getting the corresponding offset for my system's default timezone (America/Sao_Paulo): before 1914 it was -03:06:28.
In London (which I'm assuming it's your default timezone), before 1847-12-01 the offset was -00:01:15 (calculated by lat/lon, see again Matt's comment for more details), and after that it was changed to +00:00 (that' why it works for dates in 1848).
I've made a test setting the timezone to Europe/London:
options = {
year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric',
hour12: false, timeZoneName: 'long', timeZone: 'Europe/London'
};
var workingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1848 01:00:00 GMT+0100 (BST)'));
var notWorkingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1847 01:00:00 GMT+0100 (BST)'));
The result was:
11/05/1848, 00:00:00 GMT
10/05/1847, 23:58:45 GMT-00:01:15
Which confirms that before December/1847, dates had a different offset.
One way to fix it, is to consider the date as UTC:
options = {
timeZone: 'UTC'
};
var workingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1848 01:00:00 GMT+0100 (BST)'));
var notWorkingDate = Intl.DateTimeFormat('en-GB', options).format(new Date('Fri May 11 1847 01:00:00 GMT+0100 (BST)'));
The values will be:
11/05/1848
11/05/1847
Related
I have a setting for a date format that looks like:
const shortMonth12 = {
year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', hour12: 'true' };
Which will give me the following date format:
console.log(date.toLocaleString('en-US', shortMonth12)) // "Mar 28, 2022, 01:55 PM"
Great, it works and we have the date format we want. We dont want to change it.
But we also want to also translate the month name. But problem is the format itself also changes when providing another locale. For example:
console.log(date.toLocaleString('sv-SE', shortMonth12)); // "28 mars 2022 01:55 em"
So, we want to use the locales to translate the months, but also keep the formatting the same. Is it possible the way the implementation looks like?
So, we want to use the locales to translate the months, but also keep the formatting the same.
No, since toLocaleString uses the provided locale to format the date, the outcome is depending on the locale.
If you use 2 different locale's that have different date formats, like en-US and sv-SE you'll get different formats, as intended.
en-US: Mar 28, 2022, 02:19 PM
sv-SE: 28 mars 2022 02:19 em
You can get the desired outcome by creating the string manual, this requires some more logic and goes against the idea behind toLocaleString.
Use the functions like toLocaleTimeString and getFullYear to create variables based on the locale, then create a string with the desired format, for example:
const customFormat = (date, locale) => {
let d = date.getDate(),
m = date.toLocaleString(locale, { month: 'long' }),
y = date.getFullYear(),
t = date.toLocaleTimeString(locale, { hour: '2-digit', minute: '2-digit', hour12: 'true' });
return `${d} ${m}, ${y}, ${t}`;
}
const d = new Date();
const d1 = customFormat(d, 'en-US');
const d2 = customFormat(d, 'sv-SE');
console.log(d1); // 28 March, 2022, 02:25 PM
console.log(d2); // 28 mars, 2022, 02:25 em
var date = new Date();
date.setDate(date.getDate() + 1)
console.log(`streak=0; expires=${date.toLocaleString('en-us', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' })}`);
In my output, I get
streak=0; expires=Sunday, January 2, 2022
My question is:
How can I format the date like this:
Thu, 18 Dec 2013 12:00:00 UTC
(12 and UTC don't need to be included)
I want the date number to go first then the month, not the other way around.
I am doing this because I need it to be formatted like that for a cookie:
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC";
(w3schools)
Tell me if that's not how you use cookies.
From the server I am getting a DateTime in this format: 2019-07-11T05:33:53.45 and it is in UTC time.
I want to convert it to the current user’s browser time using JavaScript in the format like 11th July 2019 11:03 AM which is UTC+0530 for Indian time for given time(2019-07-11T05:33:53.45).
I have tried Intl.DateTimeFormat and also other JavaScript date time functions eg. toLocaleString. But I am getting the time in UTC format only. When I get the time as what I want UTC+0530 is also attached, which I don't want.
let d = new Date(date_from_server);
let options= { year: 'numeric', month: 'numeric', day: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric',
hour12: false };
let ddmmmyy= new Intl.DateTimeFormat('default', options).format(d);
This is one of the solution I tried. I have tried other methods also, but didn't succeed.
eg. let ddmmmyy = d.toLocaleString();
I want the time as per User's current browser timezone and in the specified format(11th July 2019 11:03 AM IST).
How can I achieve it? Please help.
You need to replace the T in the date string and also append UTC.
Then you need to remove GMT and anything after that in the result:
var dateString = "2019-07-11T05:33:53.45".replace('T', ' ') + " UTC"; // or + "Z" instead of + " UTC";
var dateObj = new Date(dateString);
var localDateString = dateObj.toString().replace(/GMT.*/g,"");
console.log(localDateString);
Try this:
var date = new Date('2019-08-14 17:00:34 UTC');
date.toString(); // outputs "Wed Aug 14 2019 13:00:34 GMT-0400 (Eastern Daylight Time)"
Due to the language barrier I am unable to find an answer to a problem I'm having.
Using 2 arrays in JavaScript I need to translate a date.
var months =['january','february','march','april','may','june','july','august','september','october'.'november','december'];
var weekday = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];
Using this I am supposed to turn for example:
Mon Oct 28 2018 14:41:20 GMT+0100
into
Monday 28 October 2018
My apologies for this likely being a repost of a question that has been answered previously, but as I previously mentioned I was unable to find it.
Use moment.js
moment(new Date('Mon Oct 28 2018 14:41:20 GMT+0100')).format('dddd DD MMMM YYYY')
You don't need external libraries in order to achieve your requirement.
You just need to use some Date functions.
var months = ['january','february','march','april','may','june','july','august','september','october','november','december'];
var weekday = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];
var date=new Date();
console.log(getDate(date));
function getDate(date){
return [
weekday[date.getDay()],
date.getDate(),
months[date.getMonth()],
date.getFullYear()
].join(' ');
}
If you want to vanilla javascript solution:
A simple solution (if you can forgo the exact full month name):
var date = new Date("Mon Oct 28 2018 14:41:20 GMT+0100");
console.log(date.toDateString());
Or
Pass some options into toLocaleDateString:
var date = new Date("Mon Oct 28 2018 14:41:20 GMT+0100");
console.log(date.toLocaleDateString("en-US", {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
}))
Check MDN Date documentation here.
I have the UTC date in this format: 2015-12-09T15:44:33.737
Currently I am doing
var ts = '2015-12-09T15:44:33.737'
var localTimeString = new Date(ts).toLocaleDateString() + ", " + new Date(ts).toTimeString();
See here: http://jsbin.com/pakoxegete/1/edit?js,console
to get 12/9/2015, 10:44:33 GMT-0500 (Eastern Standard Time)
But the format I would like is
12/09/2015, 10:44:33 AM EST
The main changes I am after is ability to go from military time to standard time with AM/PM and also to be able to convert GMT-0500 (Eastern Standard Time) to EST. The time zones only have to work for the main timezones located in the USA.
Edit: I am interested in PST, MST, CST, EST and their daytime counter parts: PDT, MDT, CDT, EDT
I guess the functions has more options that I expected. Was able to compromise and get this
var ts = '2015-12-09T15:44:33.737'
var options = { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', timeZoneName: 'short' };
console.log(new Date(ts).toLocaleDateString('en-US', options));
Which results in December 9, 2015, 10:44 AM EST