consecutive timestamp simulator in Javascript [duplicate] - javascript

This question already has answers here:
Given a start and end date, create an array of the dates between the two
(7 answers)
Closed 4 years ago.
I am looking for a way to generate a consecutive timestamp in node JS in a 1-second period. something similar to
var timeStamp = "2017-04-17T18:48:03.608Z"
for (int i=0; i< 1000000; i++) {
timeStamp = // increase in 1 second
console.log(timeStamp);
}

Convert it to a date object, and use Date.prototype.setSeconds() to add a second:
var timeStamp = "2017-04-17T18:48:03.608Z";
var time = new Date(timeStamp);
time.setSeconds(time.getSeconds() + 1);
console.log(time.toISOString());
Use Date.prototype.toISOString() to convert it back into the original format, you provided it in.

Related

Javascript date to timestamp [duplicate]

This question already has answers here:
Convert normal date to unix timestamp
(12 answers)
Closed last year.
I know there are numerous ways to go about this, but I'm dealing with date formatted as such:
"2021-01-06T16:24:34Z"
How do I convert this to a timestamp that represent post unix epoch with Javascript?
You just need to parse the date, then divide the resulting number by 1000 to have it in seconds.
To parse it, you just need to remove the T and the Z.
let dateString = "2021-01-06T16:24:34Z";
let dateForDateParsing = dateString.replace("T", " ").replace("Z", "");
console.log(dateForDateParsing);
let UnixTimestamp = Math.floor(new Date(dateForDateParsing) / 1000);
console.log(UnixTimestamp);
new Date("2021-01-06T16:24:34Z").getTime() / 1000

Javascript timestamp string to datetime and operate it [duplicate]

This question already has answers here:
Parse date without timezone javascript
(16 answers)
Closed 2 years ago.
I have an arry of strings like this "2020-04-22T09:05:28.774000+00:00", how can I convert this to a datetime and operate it with the current time? Thank you for the help
let list = ["2020-04-22T09:05:28.774000+00:00","2020-03-22T09:05:28.774000+00:00"]
let diff = [];
for(let i = 0; i < list.length; i++)
diff.push(new Date() - Date.parse(list[i]));
console.log(diff)
Pass it as an argument to new Date(string), since your string is a valid ISO format into a Date object. You can also use Date.parse(string), which will convert it to a UTC timestamp:
// Returns Date obkect
console.log(new Date('2020-04-22T09:05:28.774000+00:00'));
// Returns ms elapsed since unix epoch
console.log(Date.parse('2020-04-22T09:05:28.774000+00:00'));

Invalid date when I put a future timestamp [duplicate]

This question already has answers here:
How to convert milliseconds to date and time?
(3 answers)
Closed 5 years ago.
Suppose to have a future timestamp date.
var timestamp="1511858535000" //Future timestamp
var date=new Date(timestamp);
console.log(JSON.stringify(date));// this line give me Invalid Data
Anyone can help me to understand why?
The timestamp you mentioned in the comments works fine, but you have to pass it into the Date constructor as a number, not a string:
var timestamp = "1511858535000";
var date = new Date(Number(timestamp));
console.log(JSON.stringify(date));

Get number of days prior to the specified date Javascript [duplicate]

This question already has answers here:
Date difference in Javascript (ignoring time of day)
(15 answers)
How to subtract days from a plain Date?
(36 answers)
Closed 6 years ago.
I'm reading the value of the date input and need to workout if there is 42 days or more between the specified date and today's date. This is what I've tried so far:
var sdate = new Date($("#holiday-editor input[name=StartDate]").val()); //This is returing date in the string format
var priorDate = new Date().setDate(sdate - 42).toString(); // This is returning some abstract int value
var dateNow = new Date().getDate().toString(); // this is returning 5 even though I'd like to get today's date in the string format
if (dateNow > priorDate) {
$("#HolidayBookedLate").show();
}
If you manipulate dates a lot in your app I'd suggest to use moment.js. It's only 15kb but it has lots of useful features to work with dates.
In your case you can use diff function to get amount of days between two dates.
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

Convert a Date to a Julian javascript [duplicate]

This question already has answers here:
Calculating Jday(Julian Day) in javascript
(8 answers)
Closed 8 years ago.
I would like to convert a date such as 2014/09/30 to a Julian date. Converting a date should return an integer.
The reason why I want this integer is to use it in a subtract formula, and then revert the final results back to a date.
How can I convert a date to a Julian and then convert the final results to a date?
Date.prototype.getJulian = function() {
return Math.ceil((this / 86400000) - (this.getTimezoneOffset()/1440) + 2440587.5);
}
var valDate = input1[0];
var dt = new Date(valDate);
var julian_dt = dt.getJulian();
output1 = julian_dt;
i was able to use the code above.
Thanks
You should get familiar with javascript's Date objects. You can subtract one date object from another to find differences in time, and all kinds of other nifty things. It's definitely much cleaner, and less error-prone, than working with strings and integers.

Categories

Resources