I have some dates in some different format.
date1 = 2015-05-27T04:51:12.715Z
date2 = 2015-05-27T04:51:12.782Z
date3 = 2015-05-27T04:51:12.865Z
Dont know what this format means specially the last 4 characters.And how would I find the maximum of these dates in Javascript
Just sort the array and get the first item
var date1 = '2015-05-27T04:51:12.715Z';
var date2 = '2015-05-27T04:51:12.782Z';
var date3 = '2015-05-27T04:51:12.865Z';
var array = [date1, date2, date3];
array.sort(function(a,b) {
return new Date(a) < new Date(b);
});
var max = array[0];
Related
How can I find the minimum date when the date format is dd-MM-yyyy like below. The output should return the min date from the dates below.
var a = "12-31-2018"
var b = "12-31-2019"
You can create Date objects using those two strings, compare them and return the older one.
function olderDate(dateA, dateB) {
var date1 = new Date(dateA);
var date2 = new Date(dateB);
if (date1 < date2)
return dateA;
else
return dateB;
}
console.log(olderDate("12/31/2018", "12/31/2019"));
We can try using Date.parse to convert the date strings into bona fide dates. Then, compare them:
var a = "12/31/2018";
var b = "12/31/2019";
var dateA = Date.parse(a);
var dateB = Date.parse(b);
var dateMin = dateA < dateB ? a : b;
console.log("minimum date is " + dateMin);
function olderDate(dateA, dateB) {
var date1 = new Date(dateA);
var date2 = new Date(dateB);
if (date1 < date2)
return dateA;
else
return dateB;
}
console.log(olderDate("12/31/2018", "12/31/2019"));
I have question regarding date comparison in javascript.
I want to find max date out of three datetime variables.
Like, i can find max date in c# linq using following short way
maxDate= new[] {p1.Date, p2.Date, p3.Date}.Max();
Does javascript has such nice way to do that?
You can just use Math.max, like this:
var d1 = new Date();
var d2 = new Date();
var d3 = new Date();
var max = Math.max(d1, d2, d3);
// Will give you the max Date
console.log(new Date(max));
You can use sort method of js array to find min & max date
/* In ascending order*/
function sortDates(a, b)
{
return a.getTime() - b.getTime();
}
/* in descending order.max & min value will be different
function sortDates(a, b)
{
return b.getTime() - b.getTime();
} */
var dates = [];
dates.push(new Date("03/18/2016"))
dates.push(new Date("03/18/2015"))
dates.push(new Date("03/18/2017"))
dates.push(new Date("03/18/2018"))
var sortedArray = dates.sort(sortDates);
var minDate = sortedArray [0];
var maxDate = sortedArray [sortedArray .length-1];
console.log(minDate , maxDate);
jsfiddle
$.each(data[i].replies, function(m, n) {
var currentdate = new Date();
console.log(n.entry.date_entered);
check = moment(n.entry.date_entered, 'YYYY/MM/DD');
check1 = moment(currentdate, 'YYYY/MM/DD');
console.log(check);
console.log(check1);
var month = check.format('M');
var day = check.format('DD');
var year = check.format('YYYY');
var month1 = check1.format('M');
var day1 = check1.format('DD');
var year1 = check1.format('YYYY');
get = moment([year, month, day]);
get1 = moment([year1, month1, day1]);
g = get1.from(get);
});
Sample n.entry.date_entered : 2014-07-28 12:23:43
For all the dates i am getting a few seconds ago don't know why
I think your problem is the format mask that you pass in to moment.
In your sample you use - as the delimiter but in your format mask you use /. This way moment will not be able to parse the date and will give you the current date instead.
Try changing your format mask to "YYYY-MM-DD".
I have for example two dates:
var first = '2013-07-30';
var second = '2013-08-04';
How can i show all dates between first and second?
This should return me:
2013-07-30
2013-07-31
2013-08-01
2013-08-02
2013-08-03
2013-08-04
In PHP I can get dates to strtotime and use a while loop. But how can I do it in jQuery?
I would like have this in array.
var day = 1000*60*60*24;
date1 = new Date('2013-07-30');
date2 = new Date("2013-08-04");
var diff = (date2.getTime()- date1.getTime())/day;
for(var i=0;i<=diff; i++)
{
var xx = date1.getTime()+day*i;
var yy = new Date(xx);
console.log(yy.getFullYear()+"-"+(yy.getMonth()+1)+"-"+yy.getDate());
}
I need to calculate difference between two calendar dates. I have gone through various posts but the value returned is not correct.
Heres' my code:-
function getTotalDays()
{
var date11 = document.getElementById("departure_date").value;
var date22 = document.getElementById("arrival_date").value;
var one_day=1000*60*60*24;
var date1 = new Date(date11);
var date2 = new Date(date22);
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
var diffDays = Math.round(difference_ms/one_day);
alert(diffDays);
}
suppse the difference is 2 days its showing as 59.
What's wrong..??
The values you are passing to the date object are likely wrong. Its probably easier for you to do something like this:
var date1 = getDate(date11);
var date2 = getDate(date22);
with getDate being:
function getDate(date) {
//date format dd/mm/yyyy
var dateArr = date.split('/');
var date = new Date(dateArr[2], dateArr[1], dateArr[0]);
return date;
}