I'm going to use Moment.js to find the beginning and end of the query time.
I wrote the code as follows, and here's the result.
Code
const m = moment(); // "2023-01-18T15:59:12.064Z"
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; // "UTC"
const beginDate = moment().startOf('day').toDate();
const endDate = moment().endOf('day').toDate();
console.log(beginDate);
console.log(endDate);
Result
"2023-01-18T00:00:00.000Z"
"2023-01-18T23:59:59.999Z"
Is there any influence of local time zone in this code? The above code is the result of calling a function on the Node.js server in the KST (UTC+9) time zone.
The actual Date value is 15:59:12, so can't I set 15:00:00 as the start time?
Related
From the database I get a time as a string: 13:20:00, I get a time zone: America/New_York and a date as a string 2023-01-2 from a linked table
How do convert that time to America/Los_Angeles (would return 10:20:00) or any other time zone?
I am using the moment.js library.
I tried:
const t = "2023-01-24 13:20:00"
const tzOut = 'America/New_York'
const tzIn = "America/Los_Angeles"
const m = moment(t, "YYYY-MM-DD H:mm:SS").tz(tzIn)
return m.tz(tzOut).format("YYYY-MM-DD H:mm:SS");
// Returns "2023-01-24 16:20:00"
And this works, but if I change the time zones around:
const t = "2023-01-24 13:20:00"
const tzIn = 'America/New_York'
const tzOut = "America/Los_Angeles"
const m = moment(t, "YYYY-MM-DD H:mm:SS").tz(tzIn)
return m.tz(tzOut).format("YYYY-MM-DD H:mm:SS");
// Returns "2023-01-24 13:20:00", should be "2023-01-24 10:20:00"
I believe this is because I am in the America/Los_Angeles time zone, moment.js is factoring this in and moment(t,"YYYY-MM-DD H:mm:SS").tz(tzIn) is becoming 16:20:00. I want it to become 13:20:00 but in America/New_York time zone.
So it seems I am on the wrong track - how should I be doing this?
Edited to add dates.
The issue is that you're confusing moment.tz() and the .tz() method on a moment object. If you need to parse a date in a specific timezone, use moment.tz() to construct a new moment object like usual, but with an extra parameter that is the timezone. If you need to convert to a different timezone, then use the .tz() method.
In your code, the first time you need use moment.tz(), while the second time you need to use the .tz() method. Change your code to this:
const t = "2023-01-24 13:20:00"
const tzIn = 'America/New_York'
const tzOut = "America/Los_Angeles"
const m = moment.tz(t, "YYYY-MM-DD H:mm:SS", tzIn)
return m.tz(tzOut).format("YYYY-MM-DD H:mm:SS")
I have a date selector and a time selector, and I'm trying to figure out how I can combine their outputs to make a single ISOString so that I can use with the google calendar API.
Here's what I've tried:
//date = 2022-05-18
//time = 14:22
const apptdate = new Date(date)
const timeSplit = time.split(':')
apptDate.setHours(timeSplit[0])
apptDate.setMinutes(timeSplit[1])
What I notice is when I console.log(apptdate) this is the output I get: 2022-05-17T18:22:00.000Z
I'm not sure why it changes the day from May 18 to May 17, and the time from 14:22 to 18:22.
Does anyone have a solution for this? Or even a completely different way of combining date and time to one string (other than using a datetime-local input format, I want to keep the date and time separate in my database).
"2022-05-18" is parsed as UTC but apptDate.setHours(timeSplit[0]) sets the local hour. So if the host has a negative offset, the local date is 17 May and the time is set to the local hour on 17 May, not UTC hour on 18 May.
Instead use setUTCHours and setUTCMinutes.
let date = '2022-05-18';
let time = '14:22';
let apptDate = new Date(date);
let timeSplit = time.split(':');
apptDate.setUTCHours(timeSplit[0]);
apptDate.setUTCMinutes(timeSplit[1]);
// 2022-05-18T14:22:00.000Z
console.log(apptDate.toISOString());
PS. There was also a typo: let apptdate then later apptDate.
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?
var oldTime = document.querySelector('#oldtime');
var currentTime = document.querySelector('#currenttime');
const FetchTimeFromDb = '2018-07-18T03:07:13.384Z';
setInterval(function() {
oldTime.innerHTML = moment().format('YYYY-MM-DD HH:mm:ss');
currentTime.innerHTML = moment(FetchTimeFromDb).format('YYYY-MM-DD HH:mm:ss');
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
<div id="oldtime"></div>
<div id="currenttime"></div>
I have a bug in my application, Where i format my start time with moment
const starttime = moment('2018-07-18T03:07:13.384Z').format('YYYY-MM-DD HH;MM:ss');
and i get the current time using const now = moment().format('YYYY-MM-DD HH:MM:ss')
where first declaration change the time entirely and keep looping in the old time. Even i reverse the steps, it gives me same result. I am keeping my start time in database using ISO8601 format and when i send to server, i convert to the format for that server. Is there proper solution to handle this kind of situation?
There is nothing wrong with the code.
I found out that in my string format there is a typo.it suppose to be ('YYYY-MM-DD HH:mm:ss') instead of ('YYYY-MM-DD HH:MM:ss') which is why always set to 07.
I am getting time from server as "19:30" but it needs to be converted to "Pacific/Easter time zone.
I have tried things like
let t = "19:30:00";
let utc = moment.utc(t);
let z = moment(utc).utcOffset(-300).format('HH:mm');
But I am going wrong somewhere.
I have seen in my dev app version that it is being converted to 14:30 which is like -5:00 hours.
So, how do I get similar result for this?
Here's something else that I tried
let t = "19:30:00";
let tt = moment.tz(t, "UTC");
let nt = tt.clone().tz("Pacific/Easter");
and I am getting nt as "19:30:00" also. so , it's not converting at all
this is the one that seems to be working.
but it's not showing the correct result
let t = "19:30:00";
let utc = moment.utc(t, 'HH:mm:ss');
let z = utc.tz('Pacific/Easter').format('HH:mm');
console.log(z);
it should show the result as 19:30 -5 hours which should be 14:30,
but it shows the result as 13:30. so, anyone knows why this is happening??
You have to use tz() function from moment-timezone.
Since your input (19:30:00) is not in ISO 8601/RFC 2822 recognized format you have to parse it using moment.utc(String, String) passing 'HH:mm:ss' as second parameter.
Then you can convert your moment object to given timezone using tz().
Please note that, even if you are providing only time, you are creating a moment object that includes date. As Default section of the docs states:
You can create a moment object specifying only some of the units, and the rest will be defaulted to the current day, month or year, or 0 for hours, minutes, seconds and milliseconds.
'Pacific/Easter' uses Daylight Saving Time (see full info here) so the conversion depends on date and DST.
If you want to use fixed offset (no DST), you can use utcOffset():
Setting the UTC offset by supplying minutes. Note that once you set an offset, it's fixed and won't change on its own (i.e there are no DST rules).
If the input is less than 16 and greater than -16, it will interpret your input as hours instead.
Here a live sample:
let t = "19:30:00";
let utc = moment.utc(t, 'HH:mm:ss');
let z = utc.tz('Pacific/Easter').format('HH:mm');
console.log(z);
// DST
console.log( moment.utc('2018-09-01 19:30:00').tz('Pacific/Easter').format('HH:mm') );
// No DST
console.log( moment.utc('2018-06-01 19:30:00').tz('Pacific/Easter').format('HH:mm') );
// Fixed offset
console.log( moment.utc('19:30:00', 'HH:mm:ss').utcOffset(-5).format('HH:mm') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.17/moment-timezone-with-data-2012-2022.min.js"></script>
You need moment-timezone to get this working.
var newYork = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");