Javascript date to timestamp [duplicate] - javascript

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

Related

How to covert date time to local date time using timezone string in JavaScript? [duplicate]

This question already has answers here:
Convert date to another timezone in JavaScript
(34 answers)
Closed 12 months ago.
I want to convert given Date time to local Date time, when input Date time and timeZone is given separately as below
Date -20220120080000 ('yyyymmddHHMMSS')
timezone string -("Australia/Perth")
How to convert above timezone to local Date time without using moment library ?
If you are able to use moment, this becomes pretty straightforward.
Check this working example.
let date = "20220120080000";
const format = "YYYYMMDDHHmmss";
let tz = "Australia/Perth";
const remoteTime = moment.tz(date, format, tz);
const localTime = moment(remoteTime).local();

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

how to convert particular time into UTC timestamp [duplicate]

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
How to set Hours,minutes,seconds to Date which is in GMT
(3 answers)
Closed 5 years ago.
I'm having a time in this format like
var time = "22:00:00"
I need to convert this into UTC time format like 1567890764
I'm using this one to convert time into UTC.
Math.floor(new Date().getTime() / 1000) //it return current UTC timestamp
how to pass the "time" variable and get UTC timestamp for that particular time?
You can just create UTC Timestamps with the full date.
You can use the .getTime() function of the Date object. You will get the milliseconds since 1970/01/01 and then divide it with 1000 to get the seconds.
let datestring = "2017-10-21 13:22:01";
let date = new Date(datestring);
console.log(date.getTime() / 1000); // will return 1508584921
You can also take a look at Moment.js which is great for handling Date and Time in Javascript.
EDIT:
If you want todays date 22:00:00 just init new Date and set the time like this:
let date = new Date();
date.setHours(22);
date.setMinutes(0);
date.setSeconds(0);

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.

How do I turn a timestamp into a javascript date object? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert a Unix timestamp to time in Javascript
Turn a unix timestamp into 2008-07-17T09:24:17Z
How to do that?
Unix time stamp is in seconds since epoch right? You can convert it into milliseconds (by multiplying it by 1000) and passing it to the date constructor like this to convert it to a Date object.
new Date(unixtimestamp*1000)
Then you can use the Date APIs to get parts of the date.
unix timestamp has an accuration of second, so convert to milisecond and pass to Date constructor:
var d = new Date(timestamp * 1000)
As long as it's a valid date string, you should be able to get a Date object out of it using Date.parse().

Categories

Resources