Setting Date in the Future with Javascript - javascript

I am trying to generate a future date based on a previously set date, but I am getting strange output.
var today = new Date(),
expiration = (today.getTime() + (3*60*1000),
theFuture = new Date();
//setup future time
theFuture.setDate(expiration);
console.log(theFuture);
//outputs something like:
Tue Jan d) -2147483647 20:33:52 GMT-0500 (EST)
Why is the date malformed here?
Ultimately I want to compare the dates, but something isn't right here.

The argument to setDate is the day of the month, while the return value of getTime is the number of milliseconds since Jan 1 1970. So you're setting the day of the month to something like 1437007985574, which is almost 4 billion years in the future. You get a nonsensical result because the date formatting functions aren't designed to handle such large dates, and they're overflowing internally.
Since you're using getTime to get the time in milliseconds, you should use setTime to set it the same way:
var today = new Date(),
expiration = today.getTime() + (3*60*1000),
theFuture = new Date();
//setup future time
theFuture.setTime(expiration);
alert(theFuture);

getDate() returns day of the month (between 1 and 31). Thats why setDate results in a malformed 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.

Formatting a predefined Date value to ISO format in JavaScript

I need to send a date value to the server in ISO Format "YYYY-MM-DDTHH:mm:ss.SSS[Z]" I don't need the time details so I am setting them as zero.
For that I am using the below code
var today = new Date();
var todayWithTimeAsZero = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);
I get todayWithTimeAsZero as Tue Jul 25 2017 22:06:03 GMT+0530 (India Standard Time)
Now how do I convert this date into an ISO format. I researched everywhere but no luck.
I tried var day = todayWithTimeAsZero.toISOString(); but this creates a new date object with the time values populated like so 2017-07-24T18:30:00.000Z. Also I have momentjs in my project may be in some way I can use that.
With moment.js you can get the current date as UTC, then set the time values to zero and get the ISO string:
moment() // current date
.utc() // convert to UTC
.hours(0).minutes(0).seconds(0).milliseconds(0) // set time values to zero
.toISOString() // format to ISO8601
The value of the formatted string is 2017-07-25T00:00:00.000Z.
You can also use the setUTCxxx methods of Date:
var today = new Date();
today.setUTCHours(0);
today.setUTCMinutes(0);
today.setUTCSeconds(0);
today.setUTCMilliseconds(0);
today.toISOString() will be 2017-07-25T00:00:00.000Z.
If you create a Date then zero the UTC time and get just the date, it will be a different date from the local date for the period of the timezone offset. For someone in UTC+1000 the UTC date is yesterday until 10:00. For users who are UTC-0600 it will be "tomorrow" after 18:00 (6 pm).
Anyway, without any library you can do:
var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());

Fetch Timestamp for last 12am in specific Timezone

The problem at hand is to find out the generic timestamp last time it was 12 am in a specific timezone.
e.g. I want timestamp when it was last 12 am in India (IST).
Tried the following:
var IST = new Date(new Date().getTime() + 330*60*1000)
IST.setHours(0);
IST.setMinutes(0);
IST.setSeconds(0);
IST.setMilliseconds(0);
Turns out it returns the last time it was 12 am in GMT.
When you use
new Date()
this gives the date in the current timezone.
When you change your code to:
var UTC = new Date();
UTC.setUTCHours(UTC.getUTCHours());
UTC.setUTCMinutes(UTC.getUTCMinutes());
UTC.setUTCSeconds(UTC.getUTCSeconds());
UTC.setUTCMilliseconds(UTC.setUTCMilliseconds());
now UTC is set to current UTC time. Add time difference for eventual changes in date and set time to 00:00:00
UTC = new Date(UTC.getTime() + 330*60*1000);
UTC.setUTCHours(0);
UTC.setUTCMinutes(0);
UTC.setUTCSeconds(0);
UTC.setUTCMilliseconds(0);
you will get the last UTC 12 am. Add to this the time difference with the required timezone, and you should get the right time.
var IST = new Date(UTC.getTime() + 330*60*1000);

Conversion of date into long format, How it works?

I was trying to convert date object into long format (may be in milliseconds format) as we do in java.
So to fulfill my need, after some trial and error, I found below way which works for me:
var date = new Date();
var longFormat = date*1; // dont know what it does internally
console.log(longFormat); // output was 1380625095292
To verify, I reverse it using new Date(longFormat); and it gave me correct output. In short I was able to fulfill my need some how, but I am still blank what multiplication does internally ? When I tried to multiply current date with digit 2, it gave me some date of year 2057 !! does anyone know, what exactly happening ?
The long format displays the number of ticks after 01.01.1970, so for now its about 43 years.
* operator forces argument to be cast to number, I suppose, Date object has such casting probably with getTime().
You double the number of milliseconds - you get 43 more years, hence the 2057 (or so) year.
What you are getting when you multiply, is ticks
Visit: How to convert JavaScript date object into ticks
Also, when you * 2 it, you get the double value of ticks, so the date is of future
var date = new Date()
var ticks = date.getTime()
ref: Javascript Date Ticks
getTime returns the number of milliseconds since January 1, 1970. So when you * 1 it, you might have got value of this milliseconds. When you * 2 it, those milliseconds are doubled, and you get date of 2057!!
Dates are internally stored as a timestamp, which is a long-object (more info on timestamps). This is why you can create Dates with new Date(long). If you try to multiply a Date with an integer, this is what happens:
var date = new Date();
var longFormat = date*1;
// date*1 => date.getTime() * 1
console.log(longFormat); // output is 1380.....
Javascript tries to find the easiest conversion from date to a format that can be multiplied with the factor 1, which is in this case the internal long format
Just use a date object methods.
Read the docs: JavaScript Date object
var miliseconds=yourDateObject.getMiliseconds();
If You want to get ticks:
var ticks = ((yourDateObject.getTime() * 10000) + 621355968000000000);
or
var ticks = someDate.getTime();
Javascript date objects are based on a UTC time value that is milliseconds since 1 January 1970. It just so happens that Java uses the same epoch but the time value is seconds.
To get the time value, the getTime method can be used, or a mathematic operation can be applied to the date object, e.g.
var d = new Date();
alert(d.getTime()); // shows time value
alert(+d); // shows time value
The Date constructor also accepts a time value as an argument to create a date object, so to copy a date object you can do:
var d2 = new Date(+d);
If you do:
var d3 = new Date(2 * d);
you are effectively creating a date that is (very roughly):
1970 + (2013 - 1970) * 2 = 2056
You could try the parsing functionality of the Date constructor, whose result you then can stringify:
>
new Date("04/06/13").toString()
"Sun Apr 06 1913 00:00:00 GMT+0200"
// or something
But the parsing is implementation-dependent, and there won't be many engines that interpret your odd DD/MM/YY format correctly. If you had used MM/DD/YYYY, it probably would be recognized everywhere.
Instead, you want to ensure how it is parsed, so have to do it yourself and feed the single parts into the constructor:
var parts = "04/06/13".split("/"),
date = new Date(+parts[2]+2000, parts[1]-1, +parts[0]);
console.log(date.toString()); // Tue Jun 04 2013 00:00:00 GMT+0200

Add millisecond value to today's date and display Date, And Month and Year (Date, Month and Year are each separate values)

I am trying to create a testing script in Selenium and I need to enter a date. I have figured out how to get the dates using:
storeEval var d=new Date(); d.getDate() CurrentDay
store Eval var m=new Date(); (m.getMonth()+1) CurrentMonth
storeEval var y=new Date(); y.getFullYear() CurrentYear
Now I want want to create variables for times in the past and future. I have been told I can do so using milliseconds, which is amazing but the closest I can come is this:
storeEval new Date().getTime()+604800000 //604800000- being 7 days in the future
I get back: 1350932638018 which is 7 days forward according to this amazing calculator I found.
So, how do I take the number I found and extract the date, month and year as I did for today's date.
If your future date is stored in the variable d then it should be as easy as:
var n = new Date(d);
or if it isn't stored in a variable, then maybe something like this?
var n = new Date(Date().getTime()+604800000);
And then now n is a date object and you should be able to use the .getFullYear() methods.
Take a look at this fiddle and see if it helps: http://jsfiddle.net/wVVmw/
use toDateString()
So,
var newDate = (Date().getTime()+604800000).toDateString();
should return Mon Oct 22 2012
I don't know selenium, but it looks like JavaScript.

Categories

Resources