Check if is between 2 dates [duplicate] - javascript

This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Closed 4 years ago.
I need to check whether a string is between NOW and 5 minutes ago
I've managed to get the current date + time and the 5 minutes ago, but I'm struggling on comparing this two dates.
What I have, is a class that prints a few dates and I'd need to find if one of those dates is within the past 5 minutes
HTML:
<span class="msl_info">You have responded 3 times: on 21 Sep 2018 at 10:49, 21 Sep 2018 at 10:40, 21 Sep 2018 at 10:15.</span>
JavaScript:
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
var hour = d.getHours();
var minute = d.getMinutes();
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
//var x = document.getElementById("demo");
var hour = addZero(d.getHours());
var minute = addZero(d.getMinutes());
var minuteAgo = addZero(d.getMinutes() - 5);
//x.innerHTML = h + ":" + m;
//Today minus 5 minutes
var dateFrom = curr_date + " " + m_names[curr_month] + " " + curr_year + " at " + hour + ":" + minuteAgo;
//Now
var dateTo = curr_date + " " + m_names[curr_month] + " " + curr_year + " at " + hour + ":" + minute;
console.log(dateFrom); //21 Sep 2018 at 10:38
console.log(dateTo); // 21 Sep 2018 at 10:43
This is a fiddle

Quick example about how to solve your problem:
const e = document.getElementById('msl_info');
const dates = e.innerHTML.match(/(\d{2}\s\w{3}\s\d{4}\sat\s\d{2}:\d{2})/g);
const realDates = dates.map((date) => {
const regExp = /(\d{2}\s\w{3}\s\d{4})\sat\s(\d{2}:\d{2})/;
const parsedDate = regExp.exec(date);
return Date.parse(parsedDate[1] + ' ' + parsedDate[2]);
});
function isDateInsideInterval(date) {
const now = new Date().getTime();
const past = new Date(now - (5 * 60 * 1000)).getTime();
return date >= past && date <= now ? true : false;
}
realDates.forEach((date) => {
console.log('Is in interval: ', isDateInsideInterval(date));
});
<span id="msl_info" class="msl_info">You have responded 3 times: on 21 Sep 2018 at 14:16, 21 Sep 2018 at 10:40, 21 Sep 2018 at 14:15.</span>
I read your span content looking for dates. I transforn the dates into Date and check the interval.

Related

Javascript - How to get date in needed format? [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 6 years ago.
Want to get Date output in 2 different formats. Here is what i have at the moment.
<p id="demo"></p>
<script>
var d = new Date(day, month);
document.getElementById("demo").innerHTML = d.toString();
</script>
Does not work for me.
With this code i want to get this output: 21 Jun
Also would like to know how to get date in this format:
Jun 21, 2016 12:00 AM
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var h = d.getHours();
var ap = "AM";
if (h > 12) {
h-=12;
ap = "PM";
}
var dateString = months[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() + " " + h + ":" + d.getMinutes() + " " + ap;
var d = new Date();
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var h = d.getHours();
var ap = "AM";
if (h > 12) {
h-=12;
ap = "PM";
}
var dateString = months[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear() + " " + h + ":" + d.getMinutes() + " " + ap;
document.getElementById("demo").innerHTML = dateString.toString();
Try This one.
JavaScript doesn't have functions to format the dates, so you'll have to do it manually. You can use this code for your first format:
<p id="demo"></p>
<script>
var d = new Date(2016, 5, 21);
document.getElementById("demo").innerHTML = getMonthName(d.getMonth()) + " " + d.getDate();
function getMonthName(month) {
var monthnames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return monthnames[month];
}
</script>
You can add the code to do any other format you want. However, you'll quickly realize the reason why we have many libraries for that purpose. So, don't reinvent the wheel and just use one of those libraries.

Java script function to Add one hour to the "DD/name of the month/YYYY HH:MM:SS" format

I have a date format like this 15/name of the month/2016 Hr:MM:SS this is coming to me as a string. I want to add one hour to the input string and display it as Local time.
I am trying to substring this but the format is not consistent as the name of the month has many characters in "November" and with "July".
Can any one please suggest how to to with this.. I am looking at regular expressions but i am not sure if reg helps...
The following will allow you to add one hour and convert to local time.
var dateString = "15/January/2016 23:59:59";
// Replace slashes
var dateStringReplaced = dateString.replace('/', ' ');
// Parse date
var date = new Date(Date.parse(dateStringReplaced));
// Add an hour
date.setHours(date.getHours() + 1);
console.log(date.toLocaleString());
https://jsbin.com/tecosovaki/1/edit?js,console
function UTCTime() {
var time = "MM/DD/YYYY HH:MM:SS AM\PM";
var months = ["Jan", 'Feb', "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var array = dtc_time.split(/[\/\s:]/);
var d = new Date(array[2], array[0] - 1, array[1], array[3], array[4], array[5], 0);
d.setHours(array[3] - 1);
var time = formatAMPM(d);
var local = d.getDate() + "/" + months[d.getMonth()] + "/" + d.getFullYear() + " " + time;
document.getElementById("demo").innerHTML = local.fontcolor("white");
}
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return strTime;
}

Javascript to display the current date and time

I have the following test Script to display the current date & time :-
document.getElementById("para1").innerHTML = formatAMPM();
function formatAMPM() {
var date = new Date();
var hours = date.getHours();
var days = date.getDay();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = date + ' ' + hours + ':' + minutes + ' ' + ampm;
return strTime;
}
which will display the following :-
Fri Aug 30 2013 16:36:10 GMT+0100 (GMT Standard Time) 4:36 pm
but i need to modify this to display only:-
Fri Aug 30 2013 4:36 pm
can anyone advice on how i can achieve this ?
Demo using Console.Log
// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// log the date in the browser console
console.log('date:', n);
// log the time in the browser console
console.log('time:',time);
Demo using a DIV
// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// find the html element with the id of time
// set the innerHTML of that element to the date a space the time
document.getElementById('time').innerHTML = n + ' ' + time;
<div id='time'></div>
Note: these functions aren't fully cross browser supported
Cross-Browser Functional
//Fri Aug 30 2013 4:36 pm
console.log(formatAMPM(new Date()));
//using your function (passing in date)
function formatAMPM(date) {
// gets the hours
var hours = date.getHours();
// gets the day
var days = date.getDay();
// gets the month
var minutes = date.getMinutes();
// gets AM/PM
var ampm = hours >= 12 ? 'pm' : 'am';
// converts hours to 12 hour instead of 24 hour
hours = hours % 12;
// converts 0 (midnight) to 12
hours = hours ? hours : 12; // the hour '0' should be '12'
// converts minutes to have leading 0
minutes = minutes < 10 ? '0'+ minutes : minutes;
// the time string
var time = hours + ':' + minutes + ' ' + ampm;
// gets the match for the date string we want
var match = date.toString().match(/\w{3} \w{3} \d{1,2} \d{4}/);
//the result
return match[0] + ' ' + time;
}
Try this:
var d = new Date(),
minutes = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes() : d.getMinutes(),
hours = d.getHours().toString().length == 1 ? '0'+d.getHours() : d.getHours(),
ampm = d.getHours() >= 12 ? 'pm' : 'am',
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
return days[d.getDay()]+' '+months[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+hours+':'+minutes+ampm;
DEMO
Updated to use the more modern Luxon instead of MomentJS.
Don't reinvent the wheel. Use a tried and tested library do this for you, Luxon for example: https://moment.github.io/luxon/index.html
From their site:
https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-method-toLocaleString
//=> 'Thu, Apr 20, 11:27 AM'
DateTime.local().toLocaleString({ weekday: 'short', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' });
(function(con) {
var oDate = new Date();
var nHrs = oDate.getHours();
var nMin = oDate.getMinutes();
var nDate = oDate.getDate();
var nMnth = oDate.getMonth();
var nYear = oDate.getFullYear();
con.log(nDate + ' - ' + nMnth + ' - ' + nYear);
con.log(nHrs + ' : ' + nMin);
})(console);
This produces an output like:
30 - 8 - 2013
21 : 30
Perhaps you may refer documentation on Date object at MDN for more information
You can try the below:
function formatAMPM() {
var date = new Date();
var currDate = date.getDate();
var hours = date.getHours();
var dayName = getDayName(date.getDay());
var minutes = date.getMinutes();
var monthName = getMonthName(date.getMonth());
var year = date.getFullYear();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = dayName + ' ' + monthName + ' ' + currDate + ' ' + year + ' ' + hours + ':' + minutes + ' ' + ampm;
alert(strTime);
}
function getMonthName(month) {
var ar = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
return ar[month];
}
function getDayName(day) {
var ar1 = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
return ar1[day];
}
EDIT: Refer here for a working demo.
(new Date()).toLocaleString()
Will output the date and time using your local format. For example: "5/1/2020, 10:35:41 AM"
The request was to format a date in this format:
Fri Aug 30 2013 4:36 pm
I strongly suggest that anyone who comes across this question should use JavaScript's Intl API to format your dates instead of trying to come up with your own preferred format.
Here's an example
let d = new Date();
let formatter = Intl.DateTimeFormat(
"default", // a locale name; "default" chooses automatically
{
weekday: "short",
year: "numeric",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric"
}
);
console.log(formatter.format(d));
The output for me, in the en-US locale, is:
Wed, Sep 30, 2020, 5:04 PM
The output for someone in Mexico (es-MX), is:
miƩ., 30 de septiembre de 2020 17:23
Why is Intl better?
It's native code, with no string manipulation, no extra frameworks required, just a browser from any time after 2013 (when this question was first posted)
Nothing to download
No frameworks to add
Native code runs faster
Intl formats dates as appropriate for the user's locale, e.g. a user in a different country who would prefer to read the year before the month would see the appropriately formatted date
Get the data you need and combine it in the String;
getDate(): Returns the date
getMonth(): Returns the month
getFullYear(): Returns the year
getHours();
getMinutes();
Check out : Working With Dates
To return the client side date you can use the following javascript:
var d = new Date();
var month = d.getMonth()+1;
var date = d.getDate()+"."+month+"."+d.getFullYear();
document.getElementById('date').innerHTML = date;
or in jQuery:
var d = new Date();
var month = d.getMonth()+1;
var date = d.getDate()+"."+month+"."+d.getFullYear();
$('#date').html(date);
equivalent to following PHP:
<?php date("j.n.Y"); ?>
To get equivalent to the following PHP (i.e. leading 0's):
<?php date("d.m.Y"); ?>
JavaScript:
var d = new Date();
var day = d.getDate();
var month = d.getMonth()+1;
if(day < 10){
day = "0"+d.getDate();
}
if(month < 10){
month = "0"+eval(d.getMonth()+1);
}
var date = day+"."+month+"."+d.getFullYear();
document.getElementById('date').innerHTML = date;
jQuery:
var d = new Date();
var day = d.getDate();
var month = d.getMonth()+1;
if(day < 10){
day = "0"+d.getDate();
}
if(month < 10){
month = "0"+eval(d.getMonth()+1);
}
var date = day+"."+month+"."+d.getFullYear();
$('#date').html(date);
<!-- //Hide From Old Browsers
var d=new Date();
var y=d.getYear();
if (y < 1000)
y+=1900;
var day=d.getDay();
var m=d.getMonth();
var daym=d.getDate();
if (daym<10)
daym="0"+daym;
var mon=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
document.write("<font size='2' color='#660000'>"+mon[m]+" "+daym+", "+y+"</font>");
// End Hide -->
Result : November 08, 2014
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!
var yyyy = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
if(h<10){h='0'+h}
if(m<10){m='0'+m}
if(s<10){s='0'+s}
onload = function(){
$scope.currentTime=+dd+'/'+mm+'/'+yyyy+' '+h+':'+m+':'+s;
}
var today = new Date();
var day = today.getDay();
var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday ", "Thursday", "Friday", "Saturday"];
console.log("Today is : " + daylist[day] + ".");
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var prepand = (hour >= 12) ? " PM " : " AM ";
hour = (hour >= 12) ? hour - 12 : hour;
if (hour === 0 && prepand === ' PM ') {
if (minute === 0 && second === 0) {
hour = 12;
prepand = ' Noon';
} else {
hour = 12;
prepand = ' PM';
}
}
if (hour === 0 && prepand === ' AM ') {
if (minute === 0 && second === 0) {
hour = 12;
prepand = ' Midnight';
} else {
hour = 12;
prepand = ' AM';
}
}
console.log("Current Time : " + hour + prepand + " : " + minute + " : " + second);
<script>
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
if (h < 10) { h = '0' + h }
if (m < 10) { m = '0' + m }
if (s < 10) { s = '0' + s }
var ctoday = dd + '/' + mm + '/' + yyyy+ '\t' +h+ ':' +m+ ':' +s;
var d = new Date()
var weekday = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
console.log("Today is " + weekday[d.getDay()])
document.getElementById('time').innerHTML = '<span style="color:blue">' + weekday[d.getDay()] + ", " + ctoday + '</span>';
</script>
<div>
<span> Today is : <span id="time"> </span>
</div>
function showTime(){
let date = new Date();
let h = date.getHours();
let m = date.getMinutes();
let s = date.getSeconds();
let d = date.getDate() ;
let month = date.getMonth()+1;
let year = date.getFullYear();
let session = "AM";
if(h == 0){
h = 12;
}
if(h > 12){
h = h - 12;
session = "PM";
}
h = (h < 10) ? "0" + h : h;
m = (m < 10) ? "0" + m : m;
s = (s < 10) ? "0" + s : s;
d = (d < 10) ? "0" + d : d;
//Adds zero when less than 10.
month = (month < 10) ? "0" + month : month;

Parsing date and time from formatted date

I have a date in the following format:
Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)
Can anyone tell me how to parse the date
Wed Jul 17 2013
and time out of this without using string functions
1:00pm
One of the simplest ways is to make sure your LOCALES are set properly, which allows you to do something like this:
var myStrDate = 'Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)';
var myDate = Date.parse(myStrDate);
var myDateOnly = myDate.toLocaleDateString();
var myTimeOnly = myDate.toLocaleTimeString();
Here's a great place to start: http://www.w3schools.com/jsref/jsref_obj_date.asp
You can construct a Date object with the date string, combined with additional functions to get the required output. dayname() returns the dayname, monthname() returns the three-letter month name.
I've used console.info() for outputting results - alert() can be used as an alternative if appropriate.
function dayname(d)
{
var names = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri","Sat" ];
return names[d.getDay()];
}
function monthname(d)
{
var names = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
return names[d.getMonth()];
}
// construct new date object passing date string as parameter
var d = new Date("Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)");
console.info(dayname(d) + ' ' + monthname(d) + ' ' + d.getDate() + ' ' + d.getFullYear());
For the time, this gettime() function checks the hour to output am or pm correctly, and for correct formatting-:
function gettime(d)
{
var ampm = "am";
var hour = d.getHours();
var mins = d.getMinutes();
if (hour >= 12)
{
hour = hour - 12;
ampm = "pm";
}
if (hours < 10)
hours = "0" + hours;
if (mins < 10)
mins = "0" + mins;
return hour + ":" + mins + ampm;
}
console.info(gettime(d));
var ms = Date.parse(new Date("Wed Jul 17 2013 13:00:00 GMT-0700 (PDT)"));
var curr_date = ms.getDate();
var curr_month = ms.getMonth() + 1; //Months are zero based
var curr_year = ms.getFullYear();
var hours = ms.getHours();
var mins = ms.getMinutes();
suf = (hours >= 12)? 'pm' : 'am';
hours = hours % 12;
document.write(curr_date + "-" + curr_month + "-" + curr_year + " " + hours + ":" + min + " " + suffix);
Also try using moment.js, you can format dateTime as you wish.
Here great reference to learn date functions in javascript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Odd date behaviour

Using the following code, up until this week, the object was returning a drop-down with the "correct" weeks, i.e. week 36 as 5th september, 37 as 12th september, etc.
Since the month has changed to October, the weeks are now being returned incorrectly.
Code from LEAP.Schedule object:
/* --- LEAP Namespace --- */
var LEAP = {};
/* --- LEAP.Schedule Object --- */
LEAP.Schedule = function(){//init
this.weeks = [];
this.calculateWeeks();
};
LEAP.Schedule.prototype.calculateWeeks = function(){
this.date = new Date ( 2011, 8, 5 ); // First week of new school year
this.num = 36; // Calendar number of this week
this.weeks.push(new LEAP.Schedule.week(this.date, this.num));
for (var i = 1; i < 51; i++) {
var week = i * 7;
var updated_date = new Date ();
updated_date.setDate(this.date.getDate() + week);
if (this.num > 51) {
this.num = 0;
}
this.num++;
this.weeks.push(new LEAP.Schedule.week(updated_date, this.num));
}
};
LEAP.Schedule.prototype.getWeeks = function(){
return this.weeks;
};
/* --- LEAP.Schedule.week Object --- */
LEAP.Schedule.week = function(n_date, n_week){
this.week = n_week;
this.date = n_date;
this.year = this.date.getFullYear();
this.month = this.date.getMonth();
this.month += 1;
this.day = this.date.getDate();
var mydate = new Date(this.date);
this.end_date = mydate.setDate(mydate.getDate() + 6);
};
LEAP.Schedule.week.prototype.getJSDate = function(){
return this.date;
};
LEAP.Schedule.week.prototype.getStartDate = function(){
return this.year + "-" + pad(this.month) + "-" + pad(this.day);
};
LEAP.Schedule.week.prototype.getEndDate = function(){
end_of_week = new Date(this.end_date);
var year = end_of_week.getFullYear();
var month = pad(end_of_week.getMonth() + 1);
var day = pad(end_of_week.getDate());
return year + "-" + month + "-" + day;
};
LEAP.Schedule.week.prototype.getLabel = function(){
return "Week " + this.week + ": " + this.day + (this.day==1||this.day==21||this.day==31?"st":this.day==2||this.day==22?"nd":this.day==3||this.day==23?"rd":"th") + " " + ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][this.month-1] + " " + this.year;
};
pad = function (n) {
return n>9 ? n : "0"+n;
};
Code initialising/displaying this Object:
WeeklyUpdate.init = function() {
var Scheduleobject = new LEAP.Schedule();
var weeks = Scheduleobject.getWeeks();
var dispHTML = '<p>weeks.length: ' + weeks.length + '</p>';
for (var i = 0; i < weeks.length; i++) {
if (i % 2 > 0) {
dispHTML += '<div style="background:#ccc;">';
} else {
dispHTML += '<div style="background:#fff;">';
}
dispHTML += '<p>i: ' + i + '</p>';
dispHTML += '<p>getJSDate: ' + weeks[i].getJSDate() + '</p>';
dispHTML += '<p>getStartDate: ' + weeks[i].getStartDate() + '</p>';
dispHTML += '<p>getEndDate: ' + weeks[i].getEndDate() + '</p>';
dispHTML += '<p>getLabel: ' + weeks[i].getLabel() + '</p>';
dispHTML += '</div>';
}
$('div#wrapper').html(dispHTML);
//WeeklyUpdate.displayWeekFilter(weeks);
}
Output (trimmed after three weeks):
weeks.length: 51
i: 0
getJSDate: Mon Sep 05 2011 00:00:00 GMT+0100 (GMT Daylight Time)
getStartDate: 2011-09-05
getEndDate: 2011-09-11
getLabel: Week 36: 5th Sep 2011
i: 1
getJSDate: Wed Oct 12 2011 13:58:02 GMT+0100 (GMT Daylight Time)
getStartDate: 2011-10-12
getEndDate: 2011-10-18
getLabel: Week 37: 12th Oct 2011
i: 2
getJSDate: Wed Oct 19 2011 13:58:02 GMT+0100 (GMT Daylight Time)
getStartDate: 2011-10-19
getEndDate: 2011-10-25
getLabel: Week 38: 19th Oct 2011
I've looked through this a few times but I'm getting rather confused! Any ideas? I'm sure it's something pretty obvious.
Cheers
When you initialize a new Date() it defaults to the current date. getDate only refers to the day, not the month or year. Your code worked in September because that was the same as the start month, but since it is now October, you are offsetting from the wrong month.
var updated_date = new Date ();
should be
var updated_date = new Date (2011, 8, 5);
That way you are offsetting from the start of the school year, not today.
Demo: http://jsfiddle.net/aXgk6/
Use this code, and change the base date to the this.week variable.
I've also applied a change to the part of the code which determines a new week. Otherwise, you would be back near the end of this calender year, asking why your code is broken ;)
LEAP.Schedule.prototype.calculateWeeks = function(){
this.date = new Date ( 2011, 8, 5 ); // First week of new school year
this.num = 36; // Calendar number of this week
this.weeks.push(new LEAP.Schedule.week(this.date, this.num));
var not_new = true;
for (var i = 1; i < 51; i++) {
var week = i * 7;
var updated_date = new Date(this.date);
updated_date.setDate(this.date.getDate() + week);
if (not_new && updated_date.getYear() > this.date.getYear() ) {
not_new = this.num = 0;
}
this.num++;
this.weeks.push(new LEAP.Schedule.week(updated_date, this.num));
}
};

Categories

Resources