How to get timeZoneName as EST instead of EDT and GMT - javascript

I have requirement where i want want to show time in EST like 03:32:11 PM EST,
i tried keeping the timeZone as America/New_York but it is returning the time in EDT format
const date = new Date();
date.toLocaleString('en-US', {
timeZone: 'America/New_York',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short',
}),
output //10/21/2022, 04:32:19 PM EDT
date.toLocaleString('en-US', {
timeZone: 'EST',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short',
}),
);
output : 10/21/2022, 03:32:58 PM GMT-5
what should be the timezone to get timeZoneName as EST?

Related

Is there a way to swap dayweek and date, when formatting a date in JavaScript?

I've formatted my date like below, but I would like the weekday to go after the DD/MM/YYYY. Is this possible?
Current output: Friday, 16/06/2023, 12:00
Desired output: 16/06/2023, Friday, 12:00
const options = {
weekday: 'long',
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
};
const dt = new Date('2023-06-16T12:00:00Z').toLocaleDateString('en-GB', options)
console.log(dt)
You can do by custom code format of required output.
In your case to acheive desired result this code help you.
const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
weekday: 'long',
hour: 'numeric',
minute: 'numeric',
};
const dt = new Date('2023-06-16T12:00:00Z');
const formattedDate = `${dt.toLocaleDateString('en-GB', {
year: 'numeric',
month: 'numeric',
day: 'numeric',
})}, ${dt.toLocaleDateString('en-GB', {
weekday: 'long',
})}, ${dt.toLocaleTimeString('en-GB', {
hour: 'numeric',
minute: 'numeric',
})}`;
console.log(formattedDate);

get new Date() in other timezone in full text string format

I need to get Thailand timezone in this format: Thu Nov 10 2022 14:08:37 GMT+0800 (Malaysia Time). I have tried new Date().toLocaleString("en-US", {timeZone: "Asia/Bangkok"}) but didn't get the correct format I want, probably because of the .toLocaleString(). Is there a simple way to do it?
As deceze suggests, you can use Intl.DateTimeFormat with suitable options to get the values you want. Then you can use formatToParts to reorganise them as you wish, e.g. to replicate the format of Date.prototype.toString for any timezone, you can use:
// Return timestamp in same format as Date.prototype.toString
// in designated timezone (IANA representative location)
function toTimezone(tz, date = new Date()) {
// Get parts except timezone name
let opts = {
year: 'numeric',
month: 'short',
day: '2-digit',
weekday: 'short',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: tz,
timeZoneName: 'shortOffset',
hour12: false
}
// To get full timezone name
let opts2 = {
hour: 'numeric',
timeZone: tz,
timeZoneName: 'long'
}
let toParts = opts => new Intl.DateTimeFormat('en', opts)
.formatToParts(date)
.reduce((acc, part) => {
acc[part.type] = part.value;
return acc;
}, Object.create(null));
let {year, month, day, weekday, hour, minute,
second, timeZoneName} = toParts(opts);
// Fix offset
let sign = /\+/.test(timeZoneName)? '+' : '-';
let [oH, oM] = timeZoneName.substr(4).split(':');
let offset = `GMT${sign}${oH.padStart(2, '0')}${oM || '00'}`;
// Get timezone name
timeZoneName = toParts(opts2).timeZoneName;
return `${weekday} ${month} ${day} ${year} ${hour}:${minute}:${second} ${offset} (${timeZoneName})`;
}
// Examples
['Australia/Adelaide',
'Asia/Bangkok',
'Asia/Kolkata',
'America/New_York',
'Pacific/Yap',
'Pacific/Pago_Pago'
].forEach(tz => console.log(toTimezone(tz)));
Support for some options like shortOffset may not be ubiquitous yet. A formatting library with timezone support is a simpler (and more reliable) option. :-)
You can configure the locale string formatter with a whole bunch of options:
console.log(new Date().toLocaleString('en-US', {
timeZone: 'Asia/Bangkok',
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
hour12: false
}));
However, the exact format it will output will always be dependent on the locale used and the browser's understanding of how dates should be formatted for that locale. If you want more control over the exact formatting, you'll need to cobble it together yourself:
const date = new Date();
const time = date.toLocaleString('en-US', {
timeZone: 'Asia/Bangkok',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
hour12: false
});
const weekday = date.toLocaleString('en-US', {
timeZone: 'Asia/Bangkok',
weekday: 'short'
});
console.log(`${weekday} ${date.getFullYear()} ... ${time}`);
If that seems too complicated, use some 3rd party library like Luxon, which can simplify that a bit.

Date.toLocaleString inadequacy for conversion of a date/time to universal time

I'm trying to convert a date object to UTC. Either I'm using it wrong or the Date.toLocaleString seems to be broken or inadequate.
new Date('Tue Aug 09 2022 18:43:00 GMT-0500 (Central Daylight Time)')
.toLocaleString('en-US', {
timeZone: "UTC",
day: "2-digit",
hour12: false,
year: "numeric",
month: "2-digit",
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
output:
"08/09/2022, 23:43:00" (A valid date/time)
new Date('Tue Aug 09 2022 19:43:00 GMT-0500 (Central Daylight Time)')
.toLocaleString('en-US', {
timeZone: "UTC",
day: "2-digit",
hour12: false,
year: "numeric",
month: "2-digit",
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
output:
"08/10/2022, 24:43:00"
I expected it to be "08/10/2022, 00:43:00" but instead it appears to not reset the hours.
I have worked around this, in the particular way I was using the result, but I would like to be able to go without the extra conditional and reset.
var parts = str.match(/(\d{2})\/(\d{2})\/(\d{4}),\s(\d{2})\:(\d{2})\:(\d{2})/),
month = parseInt(parts[1], 10),
day = parseInt(parts[2], 10),
year = parseInt(parts[3], 10),
hours = parseInt(parts[4], 10),
minutes = parseInt(parts[5], 10),
seconds = parseInt(parts[6], 10);
if (hours == 24) {
hours = 0;
}
Why is this happening, and how can I accomplish my goal?
Use hourCycle: "h23" instead of hour12: false in your options. Read more about the options on MDN.
console.log(
new Date('Tue Aug 09 2022 19:43:00 GMT-0500 (Central Daylight Time)')
.toLocaleString('en-US', {
timeZone: "UTC",
day: "2-digit",
hourCycle: "h23",
year: "numeric",
month: "2-digit",
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
);
.as-console-wrapper { max-height: 100% !important; height: 100%; }
I suppose it's the americain way of displaying time after midnight when using 24-hour (non 12 hour) time. Try using en-UK for locale:
console.log(
new Date('Tue Aug 09 2022 19:43:00 GMT-0500 (Central Daylight Time)')
.toLocaleString('en-UK', {
timeZone: "UTC",
day: "2-digit",
hour12: false,
year: "numeric",
month: "2-digit",
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
);

Js Date - Unix Time Stamp wrong result

Hi I have generated a Unix Time Stamp with an online generator and I choose that Date:
01/12/2020 # 9:30pm (UTC)
Which gave me result: 1578864600
I want to display it now in my react app: (date is my result above)
const timestamp = new Date().setMilliseconds(date);
const formattedDate = new Intl.DateTimeFormat('pl-PL', {
year: 'numeric',
month: 'numeric',
day: 'numeric',
weekday: 'long',
hour: 'numeric',
minute: 'numeric',
}).format(timestamp);
But in my react app I am getting that wrong Date:
Tuesday, 14.01.2020, 17:07
Why it is calculating it wrong??

How to get the day number with JS?

I have this function below that gets the Day, month and year, but I would like it to also show the number. Right now it gives me: "February 2018 Saturday". I would like it to say: "February 2018 Saturday 17". The number is for the days date.
function:
window.onload = function() {
var date = new Date();
document.getElementById("date").innerHTML = date.toLocaleString('en-US', {weekday: 'numeric', weekday: 'long', month: 'long', year: 'numeric'});
}
html:
<div id="date"> </div>
can try this
var event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(event.toLocaleDateString( options));
Is missing the option day: 'numeric'. However, the format changes.
window.onload = function() {
var date = new Date();
document.getElementById("date").innerHTML = date.toLocaleString('en-US', {
weekday: 'numeric',
weekday: 'long',
month: 'long',
year: 'numeric',
day: 'numeric'
});
}
<div id="date"> </div>
window.onload = function() {
var date = new Date();
document.getElementById("date").innerHTML = date.toLocaleString('en-US', {
weekday: 'numeric',
weekday: 'long',
month: 'long',
year: 'numeric',
day: 'numeric'
});
}
<div id="date"> </div>

Categories

Resources