Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How does js get the time of the week and date?
How does js get the time of the week and date?
& lt scripting language = "javascript">

//This method will get the date of each week of the year.

Function getXDate (year, week, working day) {

//Construct a date object of the specified year and set the date to 1+0 this year.

//Because the month in the computer starts from 0, there are several construction methods.

var date = new Date(year," 0 "," 1 ");

//Gets the long integer time of the date of the date object.

var time = date . gettime();

//Add this long shaping time to the time offset of the nth week.

//Because the first week is the current week, there are: weeks- 1, and so on.

//7*24*3600000 is the number of milliseconds in a week (the date in JS is accurate to milliseconds).

Time+= (weeks-1) * 7 * 24 * 3600000;

//Reset the date object to time and time.

Date.setTime (time);

Returns getNextDate (date, working day);

}

//This method will get the date (working day) of the week in which the date is located.

Function getNextDate(nowDate, weekday) (

//0 is Sunday, 1 is Monday. ...

weekDay % = 7;

var day = now date . getday();

var time = now date . gettime();

Var sub = working day-day;

time+= sub * 24 * 3600000;

NowDate.setTime (time);

Return immediately;

}

//Friday of week 1 2008

Alert ("Friday of week 1 in 2008 is" +getXDate(2008, 1, 5));

//Friday, May 20081

Alert ("Friday of the 5th1week in 2008 is"+getxdate (5 1 5, 2008));

& lt/script & gt;

= = = = = = = Save the following as a file in. Html format = = = = = = = =

& lthtml & gt

& lthead & gt

& ltmeta http-equiv = " Content-Type " Content = " text/html; charset=utf8 " >

& lt scripting language = "javascript">

//This method will get the date of each week of the year.

Function getXDate (year, week, working day) {

//Construct a date object of the specified year and set the date to 1+0 this year.

//Because the month in the computer starts from 0, there are several construction methods.

var date = new Date(year," 0 "," 1 ");

//Gets the long integer time of the date of the date object.

var time = date . gettime();

//Add this long shaping time to the time offset of the nth week.

//Because the first week is the current week, there are: weeks- 1, and so on.

//7*24*3600000 is the number of milliseconds in a week (the date in JS is accurate to milliseconds).

Time+= (weeks-1) * 7 * 24 * 3600000;

//Reset the date object to time and time.

Date.setTime (time);

Returns getNextDate (date, working day);

}

//This method will get the date (working day) of the week in which the date is located.

Function getNextDate(nowDate, weekday) (

//0 is Sunday, 1 is Monday. ...

weekDay % = 7;

var day = now date . getday();

var time = now date . gettime();

Var sub = working day-day;

time+= sub * 24 * 3600000;

NowDate.setTime (time);

Return immediately;

}

//Friday of week 1 2008

//alert ("Friday of week 1 in 2008 is" +getXDate(2008, 1, 5));

//Friday, May 20081

//alert ("Friday of the 5th1week in 2008 is"+getxdate (2008,51,5));

Function alertx(){

var y = document . getelementbyid(' y ')。 Value;

var w = document . getelementbyid(' w ')。 Value;

var d = document . getelementbyid(' d ')。 Value;

var date = getXDate(y,w,d);

Alert(Y+ "week" +w+ "week" +d+ "date is: \ n"+date. tolocalestring ());

}

& lt/script & gt;

& lt/head & gt;

& ltbody & gt& lt center & gt

& lth2 & gt the year, week and week found date entered.

& ltinput type = " text " name = " y " id = " y " size = " 6 " value = " 2008 "/& gt; The first year.

& lt input type = "text" name = "w" id = "w" size = "4" value = "1"/> what week is it?

& lt input type = "text" name = "d" id = "d" size = "3" value = "6"/> What day is it today?

& ltbutton onclick = " alertx();" & gt take a chance

& lt/body & gt;