Dates between two dates - javascript

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());
}

Related

JavaScript forEach. String elements to date

I have an array of strings scheduleDates:
0:"24.04.2016, 11:53"
1:"12.04.2016, 10:07"
2:"13.04.2016, 9:45"
I need to replace the string elements to Date type in the same array.
I tried:
scheduleDates.forEach(function (date) {
var currDate = date.split(', ')[0];
var currTime = date.split(', ')[1];
var hours = currTime.split(':')[0];
var minutes = currTime.split(':')[1];
var year = currDate.split('.')[2];
var month = currDate.split('.')[1];
var day = currDate.split('.')[0];
var newDate = new Date(year, month, day, hours, minutes);
date = newDate;
});
It doesn't work
First of all you should use map instead of forEach. It better fits your purpose.
Here is what I would do (by following your initial thoughts):
function toDateObject(date) {
var currDate = date.split(', ')[0];
var currTime = date.split(', ')[1];
var hours = currTime.split(':')[0];
var minutes = currTime.split(':')[1];
var year = currDate.split('.')[2];
var month = currDate.split('.')[1];
var day = currDate.split('.')[0];
return new Date(year, month, day, hours, minutes);
}
var scheduleDates = ["24.04.2016, 11:53","12.04.2016, 10:07","13.04.2016, 9:45"].map(toDateObject);
Array.prototype.map applies the function given as argument to each element of the array and the collects all the result in a new array which is the result.
In this case it receives an array of strings, applies the toDateObject function to each of them and then returns a new array of the results of that function calls.
Read more about it here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
date is a variable that is local to every function call. You're assigning to this local variable, not the position in the array.
An ugly fix is to assign the new value to the correct index. This is a bit hacky, and generally not what forEach is intended for.
scheduleDates.forEach(function (date, index) {
var currDate = date.split(', ')[0];
var currTime = date.split(', ')[1];
var hours = currTime.split(':')[0];
var minutes = currTime.split(':')[1];
var year = currDate.split('.')[2];
var month = currDate.split('.')[1];
var day = currDate.split('.')[0];
var newDate = new Date(year, month, day, hours, minutes);
scheduleDates[index] = newDate;
});
You could instead use map, and assign the resulting array back into your variable.
scheduleDates = scheduleDates.map(function (date) {
// ...
return newDate;
});
Or roll your own destructive map, to make things a little easier.
function mapd (array, block) {
var length = array.length;
for (var i = 0; i < length; i++) {
array[i] = block(array[i], i);
}
}
mapd(scheduleDates, function (date) {
// ...
return newDate;
});
var scheduleDates = ["24.04.2016, 11:53","12.04.2016, 10:07","13.04.2016, 9:45"];
scheduleDates = scheduleDates.map(function (date) {
var currDate = date.split(', ')[0];
var currTime = date.split(', ')[1];
var hours = currTime.split(':')[0];
var minutes = currTime.split(':')[1];
var year = currDate.split('.')[2];
var month = currDate.split('.')[1];
var day = currDate.split('.')[0];
return new Date(year, month-1, day, hours, minutes);
});

Finding max of dates

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];

Time from X not working as expected moment.js

$.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".

Calculate difference between two calendar dates in javascript

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;
}

How to convert a string to a 24hr time?

So I have a JSON returning times like "10:00am"> i need to create an array with eight other times from the current system time. so it would go like "10:00am, 11:00am, 12:00pm, 1:00am, etc"
Here's my current code so far:
var katie=new Array();
var webdate = new Date().getHours();
var firsthr = day.date.start.time;
for (i=0; i<=8; i++){
katie{i] = webdate;
webdate = webdate +1;
}
You can try like this .
var str=new Array();
var webdate = new Date();
for (i=0; i<=8; i++){
webdate.setHours(i);
str[i] = webdate;
}
Date.js is a very helpful library for parsing strings into dates.
On this api doc page for Date.js search for ".parse". Also, the FormatSpecifiers page is helpful for converting dates to a string.
You said you have a JSON with the string, "10:00am". Assuming this JSON,
var data = {time: "10:00am"};
You could create a date object like this:
var date = Date.parse(data.time);
// this will create a date object with today's date and time set to 10am
Then, you can use your loop to increment the time to hours past 10am. Altogether it could look like this
<script src="date.js"></script>
<script>
var data = {time: "10:00am"};
var date = Date.parse(data.time);
//push today # 10am
var katie=new Array(new Date(date));
for (i=0; i<=8; i++){
//add an hour to date, then push new Date object based on date
date.add({hours:1});
katie.push(new Date(date));
}
</script>
You could try this -
var str=new Array();
var webdate = new Date();
var currenthours = webdate.getHours();
var looplimit = currenthours + 8;
for (i=currenthours; i<looplimit; i++){
webdate.setHours(i);
str[i] = webdate.getHours() >= 12 ? webdate.getHours() -12 +':00pm' : webdate.getHours() +':00am';
}
}

Categories