Javascript next and previous day function - javascript

I am developing a daily calendar and need to traverse to the next and previous day. How would I write this in javascript? Will the following calculate correctly when going to the next / previous month? Like C#'s DateTime.Today.AddDays(1) will?
new Date(year, month, day + 1)
My concern is that if I execute this on March 31st, it will calculate March 32nd...which wouldn't work obviously.
If someone could provide a function to do both that would be great!
Thanks in advance!

var dateString = '30 Apr 2010'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1); // create new increased date

You don't need to worry. It won't.
date = new Date(2012, 2, 31 + 1);
console.log(date);

Related

Why does javascript subtract an entire month rather than one day if the time isn't zeroed out?

Not looking for solutions, just want to know why I get those results and get a better understanding of javascript.
I'm calculating dates and date strings for today through 3 days ago. If I zero out time for today, everything works just fine. If I don't (and maybe also it's close to midnight UTC) yesterday is a month ago, other previous day calculations work as expected.
Screenshot:
Code run in the console at about 5:30p eastern which is 11:30p UTC
Here's the code so you can run it yourself.
Zero out time and yesterday is yesterday:
let today = new Date();
today.setUTCHours(0,0,0); // remove time, otherwise yesterday if based off of today could be a month ago.
let todays_date_string = today.toISOString().slice(0, 10);
let yesterday = new Date(todays_date_string);
yesterday.setDate(today.getDate() - 1);
let yesterdays_date_string = yesterday.toISOString().slice(0, 10);
let two_days_ago = new Date(yesterdays_date_string);
two_days_ago.setDate(two_days_ago.getDate() - 1);
let two_days_ago_date_string = two_days_ago.toISOString().slice(0, 10);
let three_days_ago = new Date(two_days_ago_date_string);
three_days_ago.setDate(three_days_ago.getDate() - 1);
let three_days_ago_date_string = three_days_ago.toISOString().slice(0, 10);
console.log({today, today_s: today.toISOString(), todays_date_string, yesterdays_date_string, two_days_ago_date_string, three_days_ago_date_string});
Don't zero out time and yesterday is a month ago:
let today = new Date();
//today.setUTCHours(0,0,0); // remove time, otherwise yesterday if based off of today could be a month ago.
let todays_date_string = today.toISOString().slice(0, 10);
let yesterday = new Date(todays_date_string);
yesterday.setDate(today.getDate() - 1);
let yesterdays_date_string = yesterday.toISOString().slice(0, 10);
let two_days_ago = new Date(yesterdays_date_string);
two_days_ago.setDate(two_days_ago.getDate() - 1);
let two_days_ago_date_string = two_days_ago.toISOString().slice(0, 10);
let three_days_ago = new Date(two_days_ago_date_string);
three_days_ago.setDate(three_days_ago.getDate() - 1);
let three_days_ago_date_string = three_days_ago.toISOString().slice(0, 10);
console.log({today, today_s: today.toISOString(), todays_date_string, yesterdays_date_string, two_days_ago_date_string, three_days_ago_date_string});
Also note that I'm using today to calculate yesterday's date rather than the newly created yesterday based off today's date string which could be a contributing factor. But I wouldn't think that would affect calculating yesterday and certainly not to that degree.
Is there an explanation for this?
The key point is this: The setDate() method changes the day of the month of a given Date instance, based on local time.
Let's walk through some code.
const date = new Date('2023-02-01T22:34:47.458Z');
const todayDateString = date.toISOString().slice(0, 10); // 2023-02-01
const todayDateDate = new Date(todayDateString);
The value of todayDateDate.toISOString() is 2023-02-01T00:00:00.000Z
The value of date.getDate() is 1.
The value of todayDateDate.toString() is Tue Jan 31 2023 19:00:00 GMT-0500 (Eastern Standard Time) (this will vary depending on your time zone).
The local date is January 31!
If you setDate(0), it gives you the last day of the previous month, based on local time. Since the local time is in January, this means December 31 in local time, and will be January 1 UTC.
The exact results depend on your local time zone.
When you construct the new date that doesn't explicitly specify a time zone, e.g. new Date('2023-02-01'), the date is interpreted in your local time zone.
But when you stringify it via toISOString() you get the date in the UTC time zone, which could be a different day (the other side of midnight).
Consider:
// February 1 in UTC+05:00
const d1 = new Date('2023-02-01T01:00:00+05:00')
// is still January 31 in UTC
console.log(d1.toISOString()); // 2023-01-31T20:00:00.000Z
So if
your timezone offset means it's still yesterday in UTC, and
yesterday was still January 31 instead of February 1, then
setDate(1) makes it January 1: a month ago.

How to compare dates without including the hour

So there is a column that has the date with the hour and i was trying to create a variable date with the same date, month, year and hour to be able to compare it wiht that date but this didn't work with me so I thought I would do that by creating the same date but when i compare i won't consider the hour but im facing some difficulties.
anyone of the two solutions would be great
I wrote many other codes but none of them worked and that was the last one i wrote
var date = new Date();
var year = date.getYear();
var month = date.getMonth() + 1; if(month.toString().length==1){var month =
'0'+month;}
var day = date.getDate(); if(day.toString().length==1){var day = '0'+day;}
var date = month+'/'+day+'/'+year;
Logger.log(date);
Im using JavaScript in google app script.
Thank you!
From MDN
We have a first step to create an object date.
let today = new Date()
let birthday = new Date('December 17, 1995 03:24:00')
let birthday = new Date('1995-12-17T03:24:00')
let birthday = new Date(1995, 11, 17) // the month is 0-indexed
let birthday = new Date(1995, 11, 17, 3, 24, 0)
let birthday = new Date(628021800000) // passing epoch timestamp
You can create your Date object following the example above that fits you better. I also recommend giving a good look into this page.
For the second step...
From there, you can use Date.now(). As explained here, this will return "A Number representing the milliseconds elapsed since the UNIX epoch."
The third step is...
comparing both numbers. Which one is smaller will be an "earlier date" and vice-versa.
If some dates don't have time, I would consider it as midnight. Using the default Date format, that would be something like this.
yyyy-mm-ddThh:mm:ssZ
Ex:
2022-02-21T09:39:23Z
The Z at the end means UTC+0.
More about this on this link.
So, a date without time would be:
2022-02-21T00:00:00Z

Choosing a specific day using "moment" javascript

Is there a way to choose a specific day using "moment" in javascript?
For example, say the date is June 20, 2020. I want to go back one month and go to the specific date the 15th (May 15th, 2020)
So far I have:
const date = moment();
date.subtract(1, 'month');
Which would give me the May part, but I'm unsure about the 15th. Thanks in advance.
const date = moment();
date.subtract(1, 'month');
date.date(15);
This should do it.
Refer here
You have an answer with moment.js, bit it's not that difficult with POJS either:
// Create a Date
let d = new Date();
// Set to 15th of previous month
d.setMonth(d.getMonth() - 1, 15);
console.log(d.toString());

How to return a Date format when I use setDate()

I assigned the weekEnd as the current end of the week like this
this.weekEnd = new Date(this.currentDate.setDate(end));
Next, what I want is to give a new value to the weekEnd which is the weekEnd + 7 days. I've done it like below but I can't assign the new value to the weekEnd because the right side returns a Number and not date.
this.weekEnd = this.weekEnd.setDate(this.weekEnd.getDate() + 7);
If you guys have any idea how I can do it I would appreciate it. Thanks a lot!
Just use the .setDate() method without the assignment:
this.weekEnd.setDate(this.weekEnd.getDate() + 7);
.setDate() does two things:
It changes the current Date object to the new date.
Returns the number of milliseconds of that new date since 1 Jan 1970.
In your code you assigned this number to the variable that would have held the correct date anyway.
Try this. It will return 15th Jun and 21st June. If you remove +1 then it will start from sunday. To start from monday you have to add 1.
let current = new Date;
let firstday = new Date(current.setDate(current.getDate() - current.getDay()+1));
let lastday = new Date(current.setDate(current.getDate() - current.getDay()+7));

JavaScript function to get yesterday date at set time as 00:00:00

I need to create a javascript function to get yesterdays date and time as midnight 00:00:00. For example 2018-08-03 00:00:00. The set.Hours() function is not working in my case. The script is to be used in spoon pentaho data integration. How i can do this. Please help
You should firstly subtract one day, then set the hours to 00:00:00, e.g.
var d = new Date();
d.setDate(d.getDate() - 1);
d.setHours(0,0,0,0);
console.log(d.toString());
If the setHours function is not working for you (i.e. not available or broken, which seems very peculiar), then you could use:
var d = new Date();
d.setDate(d.getDate() - 1);
d = new Date(d.getFullYear(), d.getMonth(), d.getDate());
console.log(d.toString());
// Or even
var e = new Date();
e = new Date(e.getFullYear(), e.getMonth(), e.getDate() - 1);
console.log(e.toString());
In Pentaho Data Integration you should always try and use the other steps before resorting to JavaScript, as the performance lowers significantly.
Use a Formula step with the function Today()(which records the current date at 00:00:00) for a column, create a colum with the value -1, and use the Calculator step with the operation DATE A + B Days.
If at some point you want the exact time of the date, you can use the function Now() in the formula step.

Categories

Resources