Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
andrew schoenherr
8,427 PointsNeed Help with JavaScript and time.
This isnt part of the Treehouse course but following it I am trying to do a few things.
I am going nuts over JavaScript and time. I have 2 things going on. First is a date picker using JqueryUI. It works as coded and alerts the selected date. On this tho I need to not allow it to selected a past date.
Second is I have created an array of times. The times need 15 minutes added to the next index item. I have created that just fine with a few problems. I set the hours to start at 8. Works great but it grabs the current minutes. I have tried to set the minutes to 0 and 1 and -1 and many others. However this does not work. The other issue is while it adds 15 minutes the current minutes once it passes the hour of 8 it repeats. Meaning it adds another 8 hour instead of turning over to 9.
Finally I need the array to be set to the date selected on the date picker. So if say someone selects October 1 2015 the array contains times in 15 minute intervals for October 1 2015 starting at 8.
I created a JsFiddle for this so that I could test many ways to make this work. I have not been able to do so yet. Any help or advice would be appreciated.
Here is the js
var Times = [];
var d = new Date();
for (var i = 0; i < 37; i++)
{
Times.push( d );
d = new Date( d.setHours(8) + 15*60*1000 ); // 15 minutes in milliseconds
}
$("#datepicker").datepicker({
dateFormat: 'MM, dd, yy',
onSelect: function(dateText)
{
var selectedDate = dateText;
alert(selectedDate);
}
});
alert(Times);
JsFiddle is here http://jsfiddle.net/andy77888/2k93x7g1/#base
1 Answer
James Anwyl
Full Stack JavaScript Techdegree Graduate 49,960 PointsEDIT: Made some changes to the code.
Hi Andrew,
Had a go and came up with something that might help. It might not work for all time zones though so you'll need to test it.
User selects a date with date picker, it logs to the console an array containing all the dates/times, in 15 minute intervals, from 08:00am to 07:45am the next day.
I've added some comments to help explain my code and will try to explain it below, but I don't completely understand it myself yet!
var Times = [];
$("#datepicker").datepicker({
// dateFormat is unix timestamp format - this is the primary format that we will work with.
dateFormat: '@',
// altFormat is the the format displayed to the user. Need to assign it to the #datepicker input with altField.
altField: '#datepicker',
altFormat: 'MM-dd-yy',
onSelect: function(dateText) {
var selectedDate = dateText;
// selectedDate = date picked @ 12:00am. Dividing by 1000 converts the date from ms to epoch?
var dividedDate = selectedDate / 1000;
// Because dividedDate = date picked @ 12:00am, we need to add 8 hours (28800000ms / 1000 = 28880) to make it date picked @ 08:00am
var startDate = dividedDate + 28800;
// There are 96 x 15min periods in 24 hours (24/96 = 0.25). We can go through each one with a loop.
for (var i = 0; i < 96; i++) {
// first time round the loop counter = 0, second time = 900...
var counter = i * 900;
// newVal = date picked + 900...1800..2700 etc.
newVal = startDate + counter;
// Convert the value to a regular date. Need to * 1000 because we had previously / 1000
var ToDate = new Date(newVal * 1000);
// creates the array. Index 0 = 08/08/2015 @ 08:00, Index 95 = 09/08/2015 @ 07:45
Times.push(ToDate);
}
// log the array to console
console.log(Times);
} // end onSelect
}); // end datePicker
Things to note:
Time zones really screw things up when it comes to dates. I am using the GMT timezone, this might not work for yours.
I am using unix time. If you've not heard of this before basically some guy started a clock on 00:00 Thursday, 1 January 1970. By counting the seconds since then we are able to work out dates/times.
I choose a starting date of 08/08/2015 (8th of August in my country). Then worked out what the unix/epoch time values are at 08:00, 08:15, 08:30, 08:45 and 09:00 (am). To do this I used the Epoch Converter
In my timezone, the values were as follows (if your using the converter above this value is the 'Epoch Timestamp'):
1439020800 (08/08/2015 08:00)
1439021700 (08/08/2015 08:15)
1439022600 (08/08/2015 08:30)
1439023500 (08/08/2015 08:45)
1439024400 (08/08/2015 09:00)
There is a difference of 900 between each value: 1439020800 (08:00) + 900 = 1439021700 (08:15)
According to google 15 minutes = 900000ms.
Doing 900000ms / 1000 = 900. - I think dividing by 1000 converts from unix time to epoch time?
So if we can get the value at 08/08/2015 08:00 (1439020800 in epoch time). Then we can just keep adding 900 to get a new value every 15 mins - 08/08/2015 08:15 (1439021700), 08/08/2015 08:30 (1439022600)... Stopping when we get to 09/08/2015 07:45.
When a user selects a date the date picker will return a value that is 1000 times larger than what we need (e.g 1439020800000). So we need to divide it by 1000 just as we did with the 900000ms. (You'll need to remove altField and altFormat to see this happening)
Hope this helps. Had fun doing it so thanks! Let me know what you think :)