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.
Related
On my server I have multiple events with a "start_date" field, this info is sent to the client with this structure:
{
id: 1,
name: 'Cooking masterclass',
start_date: '2021-06-21',
end_date: '2021-06-25',
}
When I receive the list of events on my client I want to classify them on 2 categories: Ongoing and Upcoming, so my code looks something like this
const ongoingEvents = [], upcomingEvents = []
for (const event of allEvents) {
const startDate = new Date(Date.parse(event.start_date))
const nowDate = new Date()
if (startDate < nowDate) {
ongoingEvents.push(event)
} else {
upcomingEvents.push(event)
}
}
The problem is that when I check what "startDate" and "nowDate" are it looks like this. As you can see the start date doesnt show Jun 21st but Jun 20 at 5pm instead.
startDate: Sun Jun 20 2021 17:00:00 GMT-0700 (Pacific Daylight Time)
nowDate: Sun Jun 20 2021 18:36:51 GMT-0700 (Pacific Daylight Time)
So for example even though the event starts on the 21st, on the 20th at 6:36pm it will show that the event already started in the "Ongoing" section because 6:36pm is later than 5:00pm.
What is the best way of deal with this issue?
Another issue I'm having is that I want to show in my client the Start Date of the event in this format:
Start Date: June 21
But when I use the startDate variable to format into a string like this:
const startString = startDate.toLocaleString('en-US', { month: 'short', day: 'numeric' })
const endString = endDate.toLocaleString('en-US', { month: 'short', day: 'numeric' })
I get:
Start Date: June 20
Probably is the same solution for both issues. I hope someone can guide me to the right solution.
Thanks!
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.
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
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