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'));
Related
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
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Converting a string formatted YYYYMMDDHHMMSS into a JavaScript Date object
(6 answers)
How to add 30 minutes to a JavaScript Date object?
(29 answers)
How do I format a date in JavaScript?
(68 answers)
Closed last year.
I got this string from an api
After looking at it I realized it was a dat/time
20220112201146
I then decoded it by hand to be
2022(Y)01(M)12(D)20(H)11(M)46(S)
How would I slice everything up to be Y:M:D:H:M:S?
Example:
2022:01:12:20:11:46
And then add 80 mins to it?
Extract the various parts (year, month, day, etc) via regex, transform it to ISO 8601 format, parse it to a Date instance, then add 80 minutes
const str = "20220112201146"
const rx = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/
const iso8601 = str.replace(rx, "$1-$2-$3T$4:$5:$6")
console.log("iso8601:", iso8601)
const date = new Date(iso8601)
console.log("original date:", date.toLocaleString())
date.setMinutes(date.getMinutes() + 80)
console.log("future date:", date.toLocaleString())
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
How do I format a date in JavaScript?
(68 answers)
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 1 year ago.
Let's say I have a string 2021-08-13 and want to convert this to August 13, 2021. How would you achieve this as it's not a date object.
In my mind I can think of setting each numeric month to a text version of that month and re-arrange, however seeing if there are better ways of doing this.
Simple: convert the string into a Date object and use the toLocaleString function.
If you want to get rid of the timezone so the date stays the same wherever the user is you can first convert it into an ISO string, get rid of the 'Z' in the end, and then convert it back into the Date object.
const dateString = '2021-08-13'
const localeOptions = {dateStyle: 'long'}
const dateTimezone = new Date(dateString).toLocaleString('en-US', localeOptions)
const dateWithoutTimezone = new Date(new Date(dateString).toISOString().slice(0,-1)).toLocaleString('en-US', localeOptions)
console.log(dateTimezone)
console.log(dateWithoutTimezone)
Convert your string date to a JS Date Object
let strDate = "2021-08-13";
let date = new Date(strDate);
console.log(date.toDateString())
Learn more about Date object here: JavaScript Date
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.
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