I receive a date/time value from my backend API as a string in the following format "2020-08-04T14:30" and this value is a UTC date/time.
When I convert this to Date in JavaScript -- see below -- it's showing me my time zone. Is there a way to declare a date/time value as UTC?
const myValue = "2020-08-04T14:30";
const myDate = new Date(myValue); // This date object is showing my time zone and NOT UTC
Along with the date/time value, my API also gives me a utcOffset value. I'm trying to create a simple util function that will give me a date object by applying the utcOffset value.
export const convertFromUtc = (date, utcOffset) => {
const utcDate = new Date(date);
let localDate = utcDate;
return localDate.setMinute(utcDate.getMinute() - utcOffset);
};
This code is working fine but when I inspect the utcDate object, it's showing me my local time zone. I think it should indicate the time zone as UTC. Any suggestions?
Related
I have a date string const someDate = 2023-02-13T09:00:00.000-05:00
The problem is when I'm formatting it via dayjs
dayjs(someDate).format('h:mm A')
It returns me string according to my local timezone, when I need to keep like I received.
Any way to disable converting time to local timezone in dayjs?
Yes!
You can disable converting to local timezone in dayjs by passing in the original timezone as an additional argument in the dayjs constructor.
Example:
const someDate = "2023-02-13T09:00:00.000-05:00";
const originalTimezone = someDate.slice(-6);
const formattedDate = dayjs(someDate).utcOffset(originalTimezone).format('h:mm A');
The utcOffset() method allows you to set the offset in minutes from UTC for a specific date instance. The originalTimezone constant is used to extract the timezone offset (-05:00) from the original date string someDate, and pass it to the utcOffset() method. This will ensure that the formatted date stays in the original timezone.
I have a string called stringDate that is equal to 2023-01-20T04:48:42.327000 which is a string. This is in UTC time. I live in the Pacific timezone so the following code should return 2023-01-19T20:48:42.327Z but it's returning 2023-01-20T12:48:42.327Z. Why is this happening?
let date: any = new Date(stringDate);
console.log(date);
To force new Date() to parse a date time object as UTC you can append a zero time zone offset to it.
let date: any = new Date(stringDate + '+00:00');
The issue is most likely due to the fact that JavaScript's Date object assumes that the input string is in local time, not UTC. This means that the Date object is automatically converting the input string to the local time zone.
So using .toISOString() method, which will return a string in ISO format, including the UTC offset (represented by 'Z' at the end):
let date: any = new Date(stringDate);
console.log(date.toISOString());
This will correctly parse the input string as UTC time and output the expected result 2023-01-19T20:48:42.327Z.
I have a UTC time with offset like below. I'm trying to format the UTC date time string using format function from date-fns library.
import { format } from "date-fns";
const utcDateTime = "2021-10-14T21:03:56.3256046+00:00";
const formattedDate = format(new Date(utcDateTime), "MM/dd/yyyy hh:mm");
What I'm expecting is 10/14/2021 21:03, a 24 hour time format but what I get is 10/14/2021 04:03, a converted date time for my timezone.
How to display the date and time exactly like with UTC time instead of converting the date time to local timezone?
I created a working example using CodeSandbox. Could anyone please help?
After spending a lot of time, I was able to achieve the desired result using the plain JavaScript Date object and its functions.
First, parsing the date time string and converting it to ISO string using toISOString() function.
Second, splitting the formatted date and time extracts from the ISO string.
Below is the code
const formatToUTCDateTime = (dateString) => {
const date = new Date(Date.parse(dateString));
const formattedDate = date.toISOString().split("T")[0].split("-");
const formattedTime = date.toISOString().split("T")[1].split(":");
return `${formattedDate[1]}/${formattedDate[2]}/${formattedDate[0]} ${formattedTime[0]}:${formattedTime[1]}`;
};
console.log("Result - ", formatToUTCDateTime("2021-10-14T20:03:56.3256046+00:00"));
My server returns date in CEST time zone. Is there any way to convert that date to local user date? Something like
var CEST_date = new Date( some_CEST_timestamp );
var local_time = //some code that converts CEST_date to local user date.
I've found Moment.js library but I cannot find timezone to different timezone conversion in the docs.
i have to convert date in utc from locale date time in birt.
The only problem is that the date is divide in two numeric data type like '20131012' instead for 'yyyyMMdd' and '223112'instead for 'h24:mi:ss'.
Can anyone help to convert this two data type affected from locale settings, with other two in UTC mode?
thanks for anyone just read this..
Javascript Date objects are based on a UTC time value. When methods such as date.toString are called, the local system settings are used to show a local date and time.
You can use Date.UTC to create a UTC time value, use that to create a date object, then use the date object to get a local (system) equivalent date and time.
e.g.:
var utcDate = '20131012';
var utcTime = '223112';
// Get a UTC time value
var timeValue = Date.UTC(utcDate.substring(0,4),
utcDate.substring(4,6) - 1, // Months are zero indexed
utcDate.substring(6),
utcTime.substring(0,2),
utcTime.substring(2,4),
utcTime.substring(4)
);
// Convert time value to date object
var date = new Date(timeValue);