How to convert utc date in to milliseconds - moment - javascript

I'm trying to compare two dates using moments in my ReactJs App.
before compare two days,I have to converts dates into milliseconds.
i tried valueOf() but it's not returning milliseconds.how can i get milliseconds for below formated dates?
var s_date = moment(this.props.s_date).utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]");
var e_date = moment(this.props.e_date).utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]");

var s_date = moment(this.props.s_date);
var e_date = moment(this.props.e_date);
var diff = e_date - s_date;

Try this:
moment(this.props.s_date).valueOf()
moment(this.props.e_date).valueOf()

I'm not sure how you used valueOf but by looking at the docs this should work:
var s_date = moment(this.props.s_date).utc().valueOf()
var e_date = moment(this.props.e_date).utc().valueOf()
pseudo code: e_date > s_date
After you compare dates then format like:
s_date.format("YYYY-MM-DDTHH:mm:ss.SSS[Z]")
BTW. you do not have to convert to miliseconds to compare dates. It would be enough to do:
if(s_date > e_date) {
} else {
}

Related

Momentjs seconds from now

var startdate = '02.05.2018 18:05:03';
How to find out how many minutes have passed from startdate to now?
My try
var exp = moment(startdate);
minutes = moment().diff(exp, 'minutes');
but result 124764 it not right
Parsing of date strings are not consistent among browsers. Always pass the format of the string if it is in non-ISO format to prevent unwanted bugs:
var exp = moment(startdate, 'DD.MM.YYYY HH:mm:ss');
moment().diff(exp, 'minutes');
To get the difference between two moment time objects, you can use .diff(). Then you can use any of asHours(), asMinutes(), asSeconds() to get human-readable time difference.
var start = moment('02.05.2018 18:05:03');
var end = moment('02.05.2018 18:11:03');
var duration = moment.duration(end.diff(start));
var mins = duration.asMinutes();
console.log(mins)
In your case, you can simply call moment().diff(time) since you want to get difference between the time you specify and now.
var time = moment('02.05.2018 18:05:03');
var duration = moment.duration(moment().diff(time));
var mins = duration.asMinutes();
console.log(mins)

How can I get a string date in the format "2016-07-06T10:57Z" from Date() and toISOString

I need to get a date in this format:
2016-07-06T10:57Z
Using this code I have been able to get a date in a format somewhat like I need:
var isoDate = new Date().toISOString();
2016-07-06T08:46:08.127Z
But is there a way I can remove the seconds and fraction of seconds from the date so it appears exactly like the date: "2016-07-06T10:57Z" ?
You will always want to remove the last 8 characters ('Z' included) thus you can use a function like slice
isoDate = isoDate.slice(0, -8); //Remove seconds + fractions + Z
isoDate += "Z"; //Add back the Z
You can use this way because the format returned by toISOString() will always be
YYYY-MM-DDTHH:mm:ss.sssZ
Please try
var isoDate = new Date().toISOString();
var pos = isoDate.lastIndexOf(':');
var datePart1 = isoDate.substring(0,pos);
var datePart2 = isoDate.substr(-1, 1);
var dateStr = datePart1+datePart2;
console.log(dateStr);

How to find the number of days difference using Google Script

I was trying to find the difference between two days, I'm getting NaN.
function formatDate(oldFormat,duration,timestamp){
var formattedDate = Utilities.formatDate(oldFormat, "IST","yyyy,MM,dd");
Logger.log(timestamp);
var newDate=new Date(timestamp*1000);
Logger.log(newDate);
newDate=Utilities.formatDate(newDate,"IST","yyyy,MM,dd");
Logger.log(formattedDate);
Logger.log(newDate);
var date1=new Date(formattedDate).getTime();
Logger.log(date1)
var date2=new Date(newDate).getTime();
Logger.log(date2)
var diff=daydiff(date2,date1);
Logger.log(diff); }
function daydiff(first, second) {
return (second-first)/(1000*60*60*24);}
How to find the difference between two date in days? I've date in this format :
date 1 : 2015,05,12
date 2: 2015,05,28
There is no point to use Utilities.formatDate() as it is meant to convert a normal date in to any format, not the other way round.
Also not sure what (oldFormat,duration,timestamp) stand for. You do not use duration in your script, and both dates you showed seem to be the same format.
If you are simply trying to find the difference between two dates, try this:
function formatDate(date1,date2){
date1 = new Date(fixDate(date1));
date2 = new Date(fixDate(date2));
var diff = (date2-date1)/(1000*60*60*24);
return(diff);
}
function fixDate(date){
var collector = date;
if (collector.match(",")!=null){
collector = collector.split(",");
var myString = [collector[1], collector[2], collector[0]].join("/");
return myString
}
}

JS - Compare which date is older

I want to compare two dates as which is bigger in those dates.
var date1 = 2011-9-2;
var date1 = 2011-17-06;
Can anybody say how can I do this?
You'll need to convert both strings to date objects first.
var date1 = new Date('2011-09-02');//yyyy-mm-dd format
var date2 = new Date('2011-06-17');
if(date1 > date2){
alert('date1 is bigger than date2');
}
Once you have the 2 variables as date objects you can compare them against each other (without needing to convert to milliseconds/minutes/?)
Check this link
And then do something like this:
var days = 0;
var difference = 0;
Christmas = new Date("December 25, 2005");
today = new Date();
difference = Christmas - today;
days = Math.round(difference/(1000*60*60*24));
Code source
Create Date objects from your two values (check this link) and use that to do the comparison.

Split string to get hours

var date = '28/05/2011 12:05';
var elem = date.split('');
hours = elem[0];
I have the above date format, please tell me how to split this, so that I can obtain 12 (hours) from this string?
var date = '28/05/2011 12:05';
var hrs = date.split(' ')[1].split(':')[0];
You can use a single call to split each component using a regular expression:
var date = '28/05/2011 12:05';
var elem = date.split(/[/ :]/);
alert(elem[3]); //-> 12
Working example: http://jsfiddle.net/gZ9c7/
A RegEx solution:
var myRe = /([0-9]+):[0-9]+$/i;
var myArray = myRe.exec("28/05/2011 12:05");
alert(myArray[1]); // 12
Some additional info:
Working code sample here.
About RegEx in JS.
As long as it's a consistent format:
var hours = date.split(' ')[1].split(':')[0]
is pretty easy.
when working with Dates it's better to use dedicated date/time functions:
var date = '28/05/2011 12:05';
var ms = Date.parse(date)
alert(new Date(ms).getHours())

Categories