date time is changing depending on environment - javascript

I am trying to get a range of all the days within the month (eg) January 1 - January 31 2020.
This part works however, after I do .toISOString() on the Date object the date is getting changed in the browser Chrome but not in a Node application
they are doing the same code which is:
function dateFormat(month, year) {
const startDate = new Date(year, month, 1)
console.log('startDate', startDate)
const endDate = new Date(year, startDate.getMonth() + 1, 0)
console.log('endDate', endDate)
const dto = {
monthYear: {
startDate: startDate.toISOString(),
endDate: endDate.toISOString()
}
}
console.log('dto before stringify', dto)
console.log('dto', JSON.stringify(dto))
}
dateFormat(0, 2020)
what I would like is an JSON object that looks like this:
{
"monthYear": {
"startDate": "2020-01-01T00:00:00.000Z",
"endDate": "2020-01-31T00:00:00.000Z"
}
}

The Date function basically follows the environment in which it runs.
The browser returns a function corresponding to your region,
Typically, the server returns the utc time
For this reason, the global service manages the time in utc on the server, and displays the time in the local time at the front (browser) received.

Related

explain MongoDB date format

For defining a model in node.js I am using mongoose library and I want to store the date in that.
here is the schema:
availability: [
{
day: {
type: String,
},
startTime: {
type: Date,
},
endTime: {
type: Date,
},
},
]
I am passing the value from postman like below:
{
"parkingId":"62cfa09adb8bc68f045b4d65",
"day":"Monday",
"startTime":7,
"endTime": 22
}
But it is getting saved like this from 1970. I am aware that passing a time like this will calculate time from Unix epoch But I want to save today's date or date passed by merchant user for availability
day:Monday,
startTime: ISODate("1970-01-01T00:00:00.007+00:00"),
endTime: ISODate("1970-01-01T00:00:00.022+00:00")
And also what is 007+00:00 or 022+00:00?
Should I use moment library for that?
This is unrelated to MongoDB, this is a mongoose feature.
You define startTime and endTime as Date in your schema, but you are passing a number. let's see what the mongoose code does in this scenraio:
function castDate(value) {
...
let date;
if (value instanceof Number || typeof value === 'number') {
date = new Date(value);
}
...
};
As you can see in the case the input value is a number which is the case here, the mongoose schema casts this value to date using js new Date() function which is basically:
JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.
This means when it casts new Date(7) the result is 1 January 1970 UTC + 7 mili seconds whichi is the 7 your seeing in the result 1970-01-01T00:00:00.007+00:00
Clearly in your case all you need to do is change the Date type to Number which is actually what you're trying to save.

Calculate datetime correctly in UTC from date string

I'm trying to calculate the datetime in UTC, i have the bellow code and using Luxon
weeklyDish.orderBeforeTime = timeZoneToUTC(
"Europe/Amsterdam",
year,
month,
day,
hours
);
function timeZoneToUTC(timezone, year, month, day, hours) {
const dateObj = `${year}-${month}-${day} ${hours}:00`;
const datetime = DateTime.fromFormat(dateObj, "yyyy-M-d H:mm", {
zone: timezone,
});
return datetime.toUTC().toString();
}
The code above always return the wrong hour.
How can I get the year, month, hour and return a UTC string to save in the DB?
I'm going to be migrating data that has date as string (example: "2020-12-13"), how can I convert it to UTC date and subtract days correctly?
You need to show an example to demonstrate your issue. The following shows use of Luxon's UTC and setZone methods that both seem to correctly convert a date set for "Europe/Amsterdam".
Note that the string passed to DateTime.fromISO must form a valid ISO 8601 timestamp like YYYY-MM-DDTHH.
let DateTime = luxon.DateTime;
let [tz, y, m, d, h] = ["Europe/Amsterdam", '2020', '11', '30', '12'];
let date = DateTime.fromISO(`${y}-${m}-${d}T${h}`, { zone: "Europe/Amsterdam" });
console.log(tz + '\n' + date.toString());
let dateUTC = date.setZone('UTC');
console.log('setZone to UTC\n' + dateUTC.toString());
let dateUTC2 = date.toUTC();
console.log('toUTC method\n' + dateUTC2.toString());
<script src="https://cdn.jsdelivr.net/npm/luxon#1.25.0/build/global/luxon.min.js"></script>
PS Amsterdam standard time is +1, daylight saving time is +2.
If the date is already parsed, you can use the date constructor directly. However, the constructor depends on the local timezone, luckily you can use Date.UTC instead.
The tricky part is about the timezone, which is not supported in the constructor, but it's a simple addition anyway.
So I'd wager something like so should work:
function timeZoneToUTC(timezone, year, month, day, hours) {
return new Date(Date.UTC(year, month - 1, day, hours + timezone));
}
Note: the month parameter is an index (0-based), so if you have 1=January, you need to decrease your month by one (as in my example).
Edit: uh, apparently, Date.UTC returns a timestamp, so you need to use the constructor anyway.

How to use the Time period in date time

I have a form that sends the value of year and months from an input and then while sending the value to the server I am converting that values to ISO string like that:
const toIsoString = (year, month, day) => moment(new Date(year, month - 1, day)).toISOString(true).split('.')[0];
And then in the values I am using it like this.
StartDate: toIsoString(data.StartYear, parseInt(data.StartMonth, 10), 1),
In that case It is sending the value like this:
startDate: "2021-01-01T00:00:00"
Does anybody know why the Time period is being ignored and how can I also send the time period with the year, month and date values.Any helps would be highly appreciated.Thanks...
Does anybody know why the Time period is being ignored and how can I also send the time period with the year, month and date values.Any helps would be highly appreciated.
The time isn't ignored. In the function:
const toIsoString = (year, month, day) =>
moment(new Date(year, month - 1, day)).toISOString(true).split('.')[0];
the values for hour, minute, second and millisecond are omitted so they default to 0. What time are you expecting?
If you want the current local time added to the date, then create a date and set the year, month and day to the required values without modifying the time (though I don't know why you'd want to do that).
Rather than creating a string that you then need to further process, tell moment.js the format you want:
function toIsoString (year, month, day) {
return moment(new Date().setFullYear(year, month-1, day)).format('YYYY-MM-DD HH:mm:ss');
}
console.log(toIsoString('2021','1','1'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
You can also do that without a library, see How to format a JavaScript date, e.g.:
function formatDate(year, month, date) {
let z = n => (n<10?'0':'') + Number(n);
return `${year}-${z(month)}-${z(date)} ${
new Date().toLocaleString('en',{
hour12:false,
hour:'2-digit',
minute:'2-digit',
second:'2-digit'})
}`;
}
console.log(formatDate('2021','1','01'))
Its because you are only setting year, month and date while creating moment object. You are not setting time
You should do something like
const toIsoString = (year, month, day) => {
const currDate = moment(new Date());
currDate.year(year);
currDate.month(month - 1);
currDate.date(day);
return currDate.toISOString(true).split('.')[0];
}
Or simply use set function
const toIsoString = (year, month, day) => {
const currDate = moment(new Date());
currDate.set({
'year': year,
'month': (month - 1),
'date': day
});
return currDate.toISOString(true).split('.')[0];
}

from `const now = new Date()`, how can I verify that I am before or equal a particular date at a given timezone and hour?

I have a timezone map with publishing hour in the local zone with news that must define when they should be published on a date using a date picker.
This is a new news article that is initialized with the following:
{ timeZoneId: 'Europe/Paris, releaseHour: 9, publishingDateTime: undefined } // 9 is the hour GMT+1
I want to know how can I from const now = new Date(), verify if this article should be
published today or the next day, the criteria are:
Is now before releaseHour? (is 9am GMT+1 in paris already passs or not)
If yes, then we should offer the next release slot at 9am GMT+1 + 1 day
If no, then we should use the release slot at 9am the same day
How is this possible?
This is how I have tried:
import { isBefore, isEqual } from 'date-fns';
import { utcToZonedTime } from 'date-fns-tz';
export const getNextPublishingDateTime = (now, timeZoneId, releaseHour) => {
const zoned = utcToZonedTime(now, timeZoneId);
const releaseTime = new Date(zoned.toISOString());
releaseTime.setHours(releaseHour, 0, 0, 0);
if (isBefore(zoned, releaseTime) || isEqual(zoned, releaseTime)) {
console.log('before');
return releaseTime;
}
releaseTime.setDate(releaseTime.getDate() + 1);
console.log('after');
return releaseTime;
};
But the hour returned by utcToZonedTime is not +01:00 offset, instead it is a date at my offset.
I have tried some other ideas, using moment-tz and vanilla Date, I found this task a bit complicated and hope to find help with the JS community as this look to be a normal date comparaison.
You can use the Intl object and formatToParts method to get the current time in any supported timezone using IANA representative locations. It's reasonably well supported.
E.g.
function getHour(date, loc) {
let d = new Intl.DateTimeFormat("en-EN", {
hour: 'numeric',
hour12: false,
timeZone: loc
});
return d.formatToParts(date)[0].value;
}
let loc = 'Europe/Paris';
let now = new Date();
console.log(`The current hour in ${loc} is ${getHour(now, loc)}.`);
The above is just for illustration, there should be validation of input and return values.

How to the get the beginning of day of a date in javascript -- factoring in timezone

I am struggling to find out the beginning of day factoring in timezones in javascript. Consider the following:
var raw_time = new Date(this.created_at);
var offset_time = new Date(raw_hour.getTime() + time_zone_offset_in_ms);
// This resets timezone to server timezone
var offset_day = new Date(offset_time.setHours(0,0,0,0))
// always returns 2011-12-08 05:00:00 UTC, no matter what the offset was!
// This has the same issue:
var another_approach_offset_day = new Date(offset_time.getFullYear(),offset_time.getMonth(),offset_time.getHours())
I expect when i pass a Pacific Timezone offset, to get: 2011-12-08 08:00:00 UTC and so on.
What is the correct way to achieve this?
I think that part of the issue is that setHours method sets the hour (from 0 to 23), according to local time.
Also note that I am using javascript embedded in mongo, so I am unable to use any additional libraries.
Thanks!
Jeez, so this was really hard for me, but here is the final solution that I came up with the following solution. The trick was I need to use setHours or SetUTCHours to get the beginning of a day -- the only choices I have are system time and UTC. So I get the beginning of a UTC day, then add back the offset!
// Goal is given a time and a timezone, find the beginning of day
function(timestamp,selected_timezone_offset) {
var raw_time = new Date(timestamp)
var offset_time = new Date(raw_time.getTime() + selected_timezone_offset);
offset_time.setUTCHours(0,0,0,0);
var beginning_of_day = new Date(offset_time.getTime() - selected_timezone_offset);
return beginning_of_day;
}
In JavaScript all dates are stored as UTC. That is, the serial number returned by date.valueOf() is the number of milliseconds since 1970-01-01 00:00:00 UTC. But, when you examine a date via .toString() or .getHours(), etc., you get the value in local time. That is, the local time of the system running the script. You can get the value in UTC with methods like .toUTCString() or .getUTCHours(), etc.
So, you can't get a date in an arbitrary timezone, it's all UTC (or local). But, of course, you can get a string representation of a date in whatever timezone you like if you know the UTC offset. The easiest way would be to subtract the UTC offset from the date and call .getUTCHours() or .toUTCString() or whatever you need:
var d = new Date();
d.setMinutes(d.getMinutes() - 480); // get pacific standard time
d.toUTCString(); // returns "Fri, 9 Dec 2011 12:56:53 UTC"
Of course, you'll need to ignore that "UTC" at the end if you use .toUTCString(). You could just go:
d.toUTCString().replace(/UTC$/, "PST");
Edit: Don't worry about when timezones overlap date boundaries. If you pass setHours() a negative number, it will subtract those hours from midnight yesterday. Eg:
var d = new Date(2011, 11, 10, 15); // d represents Dec 10, 2011 at 3pm local time
d.setHours(-1); // d represents Dec 9, 2011 at 11pm local time
d.setHours(-24); // d represents Dec 8, 2011 at 12am local time
d.setHours(52); // d represents Dec 10, 2011 at 4am local time
Where does the time_zone_offset_in_ms variable you use come from? Perhaps it is unreliable, and you should be using Date's getTimezoneOffset() method. There is an example at the following URL:
http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp
If you know the date from a different date string you can do the following:
var currentDate = new Date(this.$picker.data('date'));
var today = new Date();
today.setHours(0, -currentDate.getTimezoneOffset(), 0, 0);
(based on the codebase for a project I did)
var aDate = new Date();
var startOfTheDay = new Date(aDate.getTime() - aDate.getTime() % 86400000)
Will create the beginning of the day, of the day in question
You can make use of Intl.DateTimeFormat. This is also how luxon handles timezones.
The code below can convert any date with any timezone to its beginging/end of the time.
const beginingOfDay = (options = {}) => {
const { date = new Date(), timeZone } = options;
const parts = Intl.DateTimeFormat("en-US", {
timeZone,
hourCycle: "h23",
hour: "numeric",
minute: "numeric",
second: "numeric",
}).formatToParts(date);
const hour = parseInt(parts.find((i) => i.type === "hour").value);
const minute = parseInt(parts.find((i) => i.type === "minute").value);
const second = parseInt(parts.find((i) => i.type === "second").value);
return new Date(
1000 *
Math.floor(
(date - hour * 3600000 - minute * 60000 - second * 1000) / 1000
)
);
};
const endOfDay = (...args) =>
new Date(beginingOfDay(...args).getTime() + 86399999);
const beginingOfYear = () => {};
console.log(beginingOfDay({ timeZone: "GMT" }));
console.log(endOfDay({ timeZone: "GMT" }));
console.log(beginingOfDay({ timeZone: "Asia/Tokyo" }));
console.log(endOfDay({ timeZone: "Asia/Tokyo" }));

Categories