javascript conversion string to UTC date - javascript

I'm converting string to timestamp by using
var timestamp = new Date(month+"/"+day+"/"+year).getTime()/ 1000;
My question is how to set it as UTC timezone before converting to timestamp ?

Use the Date.UTC() method instead of .getTime().
var timestamp = Date.UTC(year,month,day) / 1000;
(Note: the month is expected to be from 0-11, not 1-12.)

Related

Convert string format Date(1528822800000) to date

I have a date format as follows:
var myDateString = "Date (1528822800000)"
I do not know what to call it with the name?
How can I convert it to date in javascript?
I tried using
Date (myDateString)
but the type that it returns is string rather than date.
I also tried
Date.parse (myDateString)
but it did not work.
How to convert Date (1528822800000) to date in javascript? (not use Moment.js)
This is Unix time :
From wiki :
Unix time passed 1,000,000,000 seconds in 2001-09-09T01:46:40Z. It was
celebrated in Copenhagen, Denmark at a party held by DKUUG (at
03:46:40 local time). Unix time (also known as POSIX time[citation
needed] or UNIX Epoch time1) is a system for describing a point in
time, defined as the number of seconds that have elapsed since
00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January
1970,[2].
var time = "Date (1528822800000)";
var unixtime = time.replace(/\D/g,'');
var date = new Date(Number(unixtime));
console.log(date);
It's called UNIX epoch time
You can convert it into Human date through this website
https://www.epochconverter.com/
For more information, you can read what is epoch time from that website too ("What is epoch time?")
Normally, the length is 10 if it is in seconds and 13 it is in milliseconds (*1000)
You can create date objects with new Date().
For example:
var d = new Date(milliseconds);
var myDateString = "Date (1528822800000)"
var time = myDateString.match(/([\d]+)/)
var date = new Date(parseInt(time[0]))
console.log(date.toISOString())
2018-06-12T17:00:00.000Z

Why does Date.UTC gives back a string of milliseconds

UTC is of format - 1994-11-05T08:15:30-05:00
But why does Date.UTC gives a string representing milliseconds ?
How can I achieve the same in Javascript and what are the browser compatibility cases I need to take care of while doing without external library ( say moment ).
Date.UTC returns the date in the UTC/GMT timezone, as a Unix timestamp.
What you are referring to as UTC format is ISO-8601 format, and you retrieve that with the toISOString() method. For example:
var d = new Date();
var n = d.toISOString();

Converting Moment utc time into unix time

Hello guys I was wondering how do I convert a date into Unix time stamp using the library moment.js so I can compare the oldDate with another date.
This is what I tried:
var oldDate = (moment.unix(1490632174)).format();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Documentation is a wonderful thing. Unix Timestamp (seconds)
moment().unix();
moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).
moment(1318874398806).unix(); // 1318874398
This value is floored to the nearest second, and does not include a milliseconds component.
var oldDate = moment.unix(1490632174).unix();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate.unix());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
You can use the < and > operators:
var oldDate = moment.unix(1490632174);
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
console.log(oldDate<newDate, oldDate>newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Another way you could approach this is to leverage the native Date.parse("2017-03-27T18:29:59+02:00 GMT") method in Javascript. This method parses a date string and returns Unix Time in ms.
For more info, checkout Date.parse() documentation and this Unix Time Converter that makes use of both moment and the native JS method.

Comparing Datetime with now in JavaScript

I am getting from this Url the Datetime->
http://193.70.60.44:3000/taxi_server/api/v1.0/ride_request
"requestdatetime":"2017-03-16T20:37:18.494Z"
and i want to compare it with the datetime of now.
How can i compare this format in javascript?
You could calculate the milliseconds:
var milliseconds = (new Date()).getTime() - Date.parse('2017-03-16T20:37:18.494Z');

How to substract current utc time from php iso utc?

I want to get current time in UTC, substract it from ISO UTC string which I'm getting from backend in the following format: 2016-11-29T17:53:29+0000 (ISO 8601 date, i guess?) and get an answer in milliseconds.
What is the shortest way to do it in javascript? I'm using angular 2 and typescript frontend.
You can get the current time using:
var now = Date.now()
And convert the string to a milliseconds timestamp using:
var ts = (new Date('2016-11-29T17:53:29+0000')).getTime()
And then subtract the values:
var diff = ts - now

Categories