How to compare dates without including the hour - javascript

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

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 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));

Get actual date and time with Javascript

I'm developing a website where registrations for a particular event will open on a certain date (say, January 1, 2019) and will close on another date (say, January 10, 2019). I'm using JavaScript to redirect users to the relevant pages if they try to access it on before the 1st or after the 10 of January.
My code so far:
var d = new Date();
var startDate = new Date(2019, 0, 1, 8);
var endDate = new Date(2019, 0, 10, 23, 59);
if(endDate-d<0) // Past expiration date
window.location = "register-closed.html";
else if(startDate-d > 0) // Before starting
window.location = "register-unavailable.html";
The main problem as you might have guessed is that this code takes the local date and time from the user; if I set the date on my device as 2nd January, 2019, I'm able to access the actual register page, even though it's May right now.
I feel this would be a common problem for many, but I've been unable to find any solution to this. How do I get the REAL date and time for my country (India) instead of the device time?
TL;DR
How do I get the actual date and time for a country (in my case, INDIA) using JavaScript? If I can't use vanilla JS, is there some other method to do so?
PS: If you have any solutions that can only be bypassed using methods more complicated than changing your device time, I'll readily accept them. This whole website is just for a high school event, so I don't expect any skilled hackers to spend their time on this :)
This code will get the date (as in the 30th), month, and year. This uses the new Date(); variable type. It has several uses, and you can get the output in whatever order using something like new Date(year, month, day, hours, minutes, seconds, milliseconds). That would output something like Wed May 22 2019 10:46:32 GMT-0400 (Eastern Daylight Time).
var todaysDate = new Date();
var date = todaysDate.getDate();
var month = todaysDate.getMonth();
var year = todaysDate.getFullYear();
if(date === 10 || month === 0 || year === 2019){
//January is 0 because counting starts at 0
...
}
https://www.w3schools.com/jsref/jsref_obj_date.asp
you should take a look to this topic as it seems to answer to your problem using only vanilla JS. Hope it helps :)

Javascript next and previous day function

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);

Guess full date based on partial date entered in JavaScript

Doing some code in JavaScript/jQuery and I need to have it where the user can enter a somewhat partial date string and have JavaScript figure out the full date.
For example, if the user enters "5/24" it should return "5/24/2011" (the current year). If the user enters "5/24/2004" it should return just that.
duedate = Date.parse(qadueparse);
duedatelit = new Date(duedate);
duedatelit = Number(duedatelit.getMonth()+1) + '/' +
Number(duedatelit.getDate()) + '/' + Number(duedatelit.getFullYear());
This above code works when the user supplies a full string, however it seems to screw up on me when the user types "5/24" or some other partial string. It returns a year of 2001.
Can anyone help me out?
Turning once again to StackOverflow for my programming needs; thank you, everyone, in this vibrant community for supporting each other.
The date object can accept a deconstructed date for instantiation:
var d = new Date(2011, 7, 13, 15, 59, 0); // Jul 13th, 3:59:00pm
You can rip apart the partial date and fill in the blanks yourself. However, what is 3/7? March 7th? July 3rd? Worse yet, 3/5/7 - which is year, which is month, which is day?
comment followup:
If it's always m/d, then you just split on the /, giving you m and d values.
provided = '5/24';
parts = provided.split('/');
month = parts[0]; // 5
day = parts[1]; // 24
today = new Date();
your_date = new Date(today.getFullYear(), month, day); // new date object for 2011/5/24

Categories

Resources