Comparing Datetime with now in JavaScript - 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');

Related

Javascript 'yyyy-mm-dd hh:mm:ss' to timestamp

I want to convert datetime like '2015-05-01 05:13:43' into timestamp.
Is there Any way to do it using JavaScript?
With pure JS you can try with:
new Date(Date.parse('2015-05-01 05:13:43+0000')).getTime() / 1000
It's important to add +0000 at the end of the string - otherwise browser will use your local timezone and add/remove few hours from the result.
getTime method gives you time in ms - so we have to divide it by 1000.
You can do it!
let dateToConvert = '2015-05-01 05:13:43'
let date = new Date(dateToConvert)
let timestamp = date.getTime()

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

Compare different time zone date in Jquery

How to compare dates from different time zone?
for e.g.
x = "2013-12-02T10:10:17-0400" // (timezone EST)
and compare this date to current date
var d = new Date(); // timezone(PST)
check x < d ?
When it comes to dealing with dates and times in JavaScript, I usually use Moment.js which is a library exactly for this purpose.
Its URL is http://momentjs.com/
Then you can simply parse the given string with this line:
// Parse the given datetime
var mydate = moment("2013-12-02T10:10:17-0400");
And you can also compare two different moment values:
// Compare given datetime with the current datetime
if (moment("2013-12-02T10:10:17-0400") > moment()) {
// ...
}
Or you can just convert it to a regular JavaScript Date object:
// Parse given datetime and convert to Date object
var mydate = moment("2013-12-02T10:10:17-0400").toDate();
// Compare to current datetime
if (mydate > (new Date())) {
// ...
}
Note that the unary + operator also works with moment objects just as you would expect. So +moment() outputs the same as +(new Date()).
It's also very well documented, the Moment.js docs page has a ton of examples and useful info about it.
Better convert any of the dates to a common timezone(better to have UTC)
Now convert the datetime to milliseconds
Compare the milliseconds
Hope you understand
Use this javascript library to manipulate dates in different time zones: https://github.com/mde/timezone-js
It uses the TZ database: http://en.wikipedia.org/wiki/Tz_database

JQuery datetime to milliseconds

I have a web application where I wish to send information to a database.
I have a datepicker, which lets the user select a date and formats the date as "YYYY-MM-DD". In addition to this, the users must also select a time using a timepicker which formats the time as "HH:MM". This gets concatenated into a DateTime string as "YYYY-MM-DD HH:MM".
I need to convert this into milliseconds for the datetime to be accepted as the correct format on the database (locale format of YYYY-MM-DD HH:MM:SS.mmm).
I have a tried a host of solutions found here and elsewhere to try and convert into milliseconds. Whenever I try to concat then convert I usually get a NaN error or "invalid Date" and I cannot simply add the converted milliseconds.
Is there any way of doing this in jQuery or JavaScript?
>> var datetime = new Date();
undefined
>> datetime.getTime();
1332613433314
Date.getTime() returns the number of milliseconds since 1970/01/01:
This should be handled server-side, though.
I managed to figure this one out myself. Thanks to those who answered. Its not an ideal solution, but it works.
var d = $("#date").val();
var dateParts = new Date((Number(d.split("-")[0])), (Number(d.split("-")[1]) - 1), (Number(d.split("-")[2])));
var dateis = dateParts.getTime();
var timeEnd = $("#endtime").val();
var time1 = ((Number(timeEnd.split(':')[0]) * 60 + Number(timeEnd.split(':')[1]) * 60) * 60) * 1000;
var timeStart = $("#starttime").val();
var time2 = ((Number(timeStart.split(':')[0]) * 60 + Number(timeStart.split(':')[1]) * 60) * 60) * 1000;
var dateTimeEnd = dateis + time1;
var dateTimeStart = dateis + time2;
What this basically does, is take a date from a datepicker, and a start and an endtime from a timepicker. The ajax accepts 2 datetimes, one for start, one for end. The above solution basically gets all the values from the input values, and converts it to milliseconds. It's not the best way of doing things but it is a quick fix.
I don't realise your actual question, but I've made a code that set the datepicker to a minimum selected day as today the code is as follows :
$("#datefield").datepicker({
dateFormat:"yy-mm-dd",
minDate:new Date(new Date().getTime())
});
The return value of the new Date().getTime() is the milliseconds from 1970/01/01 05:30 am (as my system)
Can you use the JavaScript Date object?
You could use it like so:
var d = new Date(yyyy, MM, dd, hh, mm, 0, 0).getTime();
You initialize the Date object and then use the getTime function to return the number of milliseconds since Jan. 1, 1970.
$("#datefield").datepicker("getDate").getTime(); will do the trick
I would do this on the server side and use the strtotime function to convert to a timestamp which will give you a number of seconds which if you really need milliseconds for some kind of script : 1 second = 1000 milliseconds.
If anything you could use jquery to validate that you'll be sending a valid date-time to the server side script before you do so.

javascript conversion string to UTC date

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.)

Categories