React format string date to date format timzone - javascript

When I call my function:
formatDate(new Date("2020-06-08T10:37:05.915+0000")
function formatDate(tstamp) {
return new Intl.DateTimeFormat("en-GB", {
year: "numeric",month: "2-digit",day: "2-digit",
hour: "2-digit", minute: "2-digit",second: "2-digit"
}).format(tstamp);
}
My output is: 08/06/2020, 12:37:05
why I got (12:37:05) instead (10:37:05) my timezone is GMT+2.

It is because the input is GMT+0000 and you are GMT+0200 which is 2 hours ahead Greenwich Mean Time. The format method formats a date without changing its time value.

Related

Javascript: How do I get the locale string to reflect the local browser time zone and not UTC on the front end? [duplicate]

This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 4 months ago.
I have a dateTime object that I'm using to reflect the status of an item. Below is the code I have for the object:
let status = new Date(Date.parse(dateTime)).toLocaleString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute:'2-digit'}))
Currently status returns, regardless of timezone: 10/19/2022, 10:13 PM
I am in US Central Time. When I console.log
new Date(Date.parse(dateTime))
I get Wed Oct 19 2022 22:13:00 GMT-0500 (Central Daylight Time).
However when I change my computer's time to US Eastern Time, I get:
Wed Oct 19 2022 22:13:00 GMT-0400 (Eastern Daylight Time).
This is not correct. I'm expecting Eastern Time to show 11:13 PM or 23:13:00. How should I modify the code to reflect the actual local browser time?
I actually figured this out with the help of this demo.
Now my code reads:
let dateTime = '2022-10-19T22:13:17';
let dateInUTC = new Date(Date.parse(dateTime)).toLocaleString() + ' UTC';
let localDate = new Date(dateInUTC);
let status = localDate.toLocaleString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
console.log(status);
If I console.log status, I get 10/19/2022, 05:13 PM. If I change my computer's time zone to Eastern, I get 10/19/2022, 06:13 PM. This is the desired result.

Javascript Date object keeps on returning the default date (Mon Jan 19 1970 19:18:18) when trying to convert unix time [duplicate]

This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Getting incorrect dates converting timestamp with new Date
(2 answers)
Convert UNIX timestamp to date time (javascript)
(4 answers)
Closed last year.
I am trying to use the date function to convert unix time that I am trying to convert to a certain format. The function successfully returns the date in the format requested, but the time is wrong. What am I missing here?
const date = (e) => {
const unixTime = 1642698000
const format = {
weekday: 'long',
day: 'numeric',
month: "2-digit",
year: "numeric"
}
return(new Date (unixTime).toLocaleString('en-US', format))
}
Expected output (app.)
Tue Jan 18 2022 17:00:00
The output recieved
Mon Jan 19 1970 19:18:18 GMT-0500 (Eastern Standard Time)
I tried following other exampled but I wasn't able to find any that would resolve this issue. I appreciate your help in advance!
The Date constructor from Javascript accepts the number of milliseconds as timestamp, not unix time (number of seconds). So, to adjust that, is just multiply the unix time by 1000.
const date = () => {
const unixTime = 1642698000 * 1000
const format = {
weekday: 'long',
day: 'numeric',
month: "2-digit",
year: "numeric"
}
return(new Date (unixTime).toLocaleString('en-US', format))
}
console.log(date())
// logs Thursday, 01/20/2022

How to format the date to (dd/mm/yyyy hh:mm:ss)

How can I convert the date below into this template (dd/mm/yyyy hh:mm:ss) ?
05/04/2021 14:52
I tried to do it that way, but I only get the time and not the date with time.
var data = new Date('05/04/2021 14:52');
var time = data.toLocaleTimeString('pt-PT', {hour12: false});
console.log(time);
You can use below script
var data = new Date('05/04/2021 14:52');
console.log(data.toLocaleString('en-GB',{hour12: false}));
Output : "04/05/2021, 14:52:00"
If you need more date-related staff than simple date formatting, you can use Moment.js.
moment().format('MMMM Do yyyy, h:mm:ss a'); // April 5th 2021, 9:16:13 pm
moment().format('DD/MM/yyyy hh:mm'); // 05/04/2021 21:18
If you need to format your date object, simply use:
moment(date).format('DD/MM/yyyy hh:mm');
Moment.js is also useful for operation on dates like days, week, month adding/subtracting, getting the start of a week, month, quarter, and many other useful operations.
This is my solution. If you want to create a advanced format, you can read more about object Intl
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl
const formatDate = new Intl.DateTimeFormat("en" , {
day: "2-digit",
month: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false
});
console.log(formatDate.format(new Date('05/04/2021 14:52')))

JS Intl.DateTimeFormat hours start at 7

When I run:
const date = Intl.DateTimeFormat('en-us', {
second: "2-digit",
minute: "2-digit",
hour: "2-digit"
}).formatToParts(0);
console.log(date);
it says that that the hour is 7, but shouldn't it be 0? I was thinking it might be something with PST being offset 7 hours from UTC but I'm not sure how to fix that.
The time value of a Date is based on UTC, but the default stringified values for Date parts are based on local time. So your assumption about PST offset is correct.
You will get the expected result if you set the output timezone to UTC:
const date = Intl.DateTimeFormat('en-us', {
second: "2-digit",
minute: "2-digit",
hour: "2-digit",
hour12: false,
timeZone: 'UTC'
}).formatToParts(0);
console.log(date);
Note that you must set hour12 to false, otherwise it will return 12 am.
If you get hour 24 instead of 0, set hourCycle to "h23". The hour cycle should be set by the language ('en-US'), however it may not. Setting the hourCycle should override the default to ensure you get 0 for midnight not 24.

Javascript Date is not formatting correctly using `toLocaleString`

I am trying to format date in 'MM/dd/yyyy' (short date format) using toLocaleString function of javascript Date. But it is not giving expected result when I change my timezone. Bellow is snippet of my code
var d1 = new Date(1954, 0, 1); // Fri Jan 01 1954 00:00:00 GMT-0900 (AKST)
var options = {
year: 'numeric',
month: '2-digit',
day: '2-digit'
};
var shortDate = d1.toLocaleString('en-US', options);
console.log(shortDate);
Above code prints 12/31/1953 instead of expected result which is 01/01/1954.
Observations/Steps:
Change timezone of local machine to Whitehorse - Canada(Pacific DayLight Time).
Run above code snippet to replicate result.
It only gets reproduced for year value less than 1968 (works fine for years greater than 1968)
For reference, uploaded recording for it (http://recordit.co/cdWMgWFLKJ)
local date time function is a conversion from UTC to locale date time. You need to change UTC time first.
so use this.
var d1 = new Date(Date.UTC(1954, 0, 1)); // Fri Jan 01 1954 00:00:00 GMT-0900 (AKST)
var options = {
year: 'numeric',
month: '2-digit',
day: '2-digit'
};
var shortDate = d1.toLocaleString('en-US', options);
console.log(shortDate);

Categories

Resources