missing something with my epoch date conversion - javascript

I'm splitting an array, when I take the middle of the array which is reporting as epoch date in milliseconds. I'm testing the date using Epoch Converter and its valid.
I run the date() object and multiply by 1000 to adjust but I'm getting year 4000. I've switched to division just to test if I'm getting too large of a number the year is correct but the day and months are wrong....I've got to be missing something simple:
var jEtrim = item.DTM.split(/[(-]/);
var date = new Date(jEtrim[1] *1000);
sample output: Thu Jan 08 44037 07:03:20 GMT-0500 (Eastern Standard Time)
Here's the jEtrim: ["/Date", "1343151455000", "0400)/"]
Thanks in advance

Date takes its argument in milliseconds already, so you will not need to multiply by 1000. You will only need to convert it to a number:
new Date(+"1343151455000")

Related

Wrong unix time from parsed DateTime

Here is code
const dateStr = '1989-11-11T03:34';
let date = new Date(dateStr);
console.log(date.toUTCString());
console.log(+date);
And output:
Fri, 10 Nov 1989 22:34:00 GMT
626740440000 <--- far future
Why unix time is in far future?
Here is Playcode link
It's not wrong.
You seem to have two issues with what you're getting:
The time isn't the same in your toISOString output.
The number you're getting from +date isn't what you expect.
But both are correct:
The string has no timezone indicator on it, so since it's a date+time string, it's parsed as local time. This is covered by the Date Time String Format section of the specification. But your output is showing GMT (because toISOString uses GMT).
626740440000 is the number of milliseconds since The Epoch, not seconds as it was in the original Unix epoch scheme. This is covered by the Time Values and Time Range section of the specification.
If you want to parse your string as UTC (loosely, GMT), add a Z to the end of it indicating that't is in GMT.
If you want the number of seconds since The Epoch, divide +date by 1000 (perhaps rounding or flooring the result).

Covert <input type="date" ng-model="jobDueDate"> to unix timestamp

Is there a way to elegantly convert the value of this particular form field (input type="date") to a UNIX timestamp?
console.log($scope.jobDueDate) gives me "Wed Jun 08 2016 00:00:00 GMT+0530 (IST)" and I really don't want to define a dictionary mapping months to numbers.
You can do it as described in this SO answer
$scope.jobDueDate.getTime()
EDIT:
getTime would give you milliseconds, which you then need to divide by 1000
$scope.jobDueDate.getTime()/1000
Just use
var unixtime = Date.parse($scope.jobDueDate)/1000;
Explanation:
Date.parse($scope.jobDueDate) Gives time in milliseconds since 1970. So you divide by a 1000 to get time in seconds.

How does minus integer work in Date?

How does javascript Date interpret the milisecond integers?
var d = new Date(-1724115600000); //this gives me a date in the past, which I want
console.log(d);
var d = new Date(1724115600000);
console.log(d);
(we had a bug where the - sign was not getting through. But I dont understand the significance of the -)
The Date object constructor can take a variety of inputs, but when called in this fashion it's using the integer value one:
Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).
Negative values will give dates before the Unix Epoch, positive values are dates after the Epoch.
0 would be 1. January 1970. The delta is given as an unsigned number representing milliseconds. If you want dates before that you need to use negative values in milliseconds.
The negative number you provided will give a number in the past, the other one in the future:
Date 1915-05-14T23:00:00.000Z
Date 2024-08-20T01:00:00.000Z
If you got one in the past with the second number it may have been missing the last digit when your tried. In that case it would give:
Date 1975-06-19T12:06:00.000Z
var d = new Date(-1724115600000); //this gives me a date in the past, which I want
document.write(d + "<br>");
var d = new Date(1724115600000); //This gives me a date in the past too.
document.write(d + "<br>");
var d = new Date(172411560000); //missing last digit
document.write(d);
//negative sign give you the date before 1970. in your example
var d = new Date(-1425223942000);// this gives date in the past
document.write(d) //Sun Nov 02 1924 03:27:38 GMT-0500 (Eastern Standard Time)
document.write('<br/>')
var d = new Date(1425223942000); //This gives date in th future.
document.write(d); // Sun Mar 01 2015 10:32:22 GMT-0500 (Eastern Standard Time)
//Unfortunately i cannot post the screenshots yet

Javascript Date MAX_VALUE

In JavaScript, what is the maximum value for the year in a Date?
How can I find this out in code?
I have tried the following:
new Date().getFullYear().MAX_VALUE;
Thanks in advance.
As per specs here: https://262.ecma-international.org/11.0/#sec-time-values-and-time-range : The actual range of times is 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC. So, the maximum valid year you will get is 275760 (new Date(8640000000000000).getFullYear()). And to get the minimum valid year, new Date(-8640000000000000).getFullYear() or 271821 B.C.E.

Converting a String Date to a Unix Timestamp in Javascript

How can I parse a date such as the following and convert it to a Unix timestamp using JavaScript?
Sat Mar 29 2014 16:10:00 GMT+0800 (Taipei Standard Time)
Thanks.
you just need a good date-parsing function, I would look at date.js . It will take just about any date string you can throw at it, and return you a JavaScript Date object.
Once you have a Date object, you can call its getTime()
method, which will give you milliseconds since January 1, 1970. Just divide that result by 1000 to get the unix
timestamp value.
In code, just include date.js, then:
var unixtime = Date.parse("24-Nov-2009 17:57:35")
.getTime()/1000
Get date.js from http://www.datejs.com/
More here: https://stackoverflow.com/a/1792009/390897

Categories

Resources