PDA

View Full Version : Get Monday of week



anykey
27 Nov 2009, 7:40 AM
Hello,

tried to calc the Monday of a given week/year and thought this should do (but did'nt):



var mow = Date.parseDate('1/48/2009','N/W/Y');
alert('Monday of week 48 in 2009 was: '+mow.format('d.m.Y'));


The only way to do this was this, little cryptic, code:

var mow = new Date().add(Date.DAY, -(new Date().getDay() - 1));
alert('Monday of week 48 in 2009 was: '+mow.format('d.m.Y'));


Any thoughts why the first one do not work?

Condor
27 Nov 2009, 8:01 AM
The following date format codes can be parsed, but are not actually used to set the date:

D A short textual representation of the day of the week Mon to Sun
l A full textual representation of the day of the week Sunday to Saturday
N ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j
w Numeric representation of the day of the week 0 (for Sunday) to 6 (for Saturday)
W ISO-8601 week number of year, weeks starting on Monday 01 to 53
t Number of days in the given month 28 to 31
L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
T Timezone abbreviation of the machine running the code Examples: EST, MDT, PDT ...

Mike Robinson
1 Dec 2009, 8:31 AM
Once you have a JavaScript "date" object, you can use any of these methods (http://www.w3schools.com/jsref/jsref_obj_date.asp) to work with the date value.

A Google search on "javascript date week" makes it very clear that you really aren't the first person to need to do what you're doing. Simply find someone else's existing code and explanation.

anykey
2 Dec 2009, 3:27 AM
Thanks a lot, i'm fine now.