The result of outputting the date directly in JavaScript is this:
function Date () {
var date = new Date ();
alert (date);
}
The result: Mon June 15:30:46 utc+0800 2009
This may not be what we need, so it is necessary to convert the next, here to learn some of my conversion methods, the wrong place please advise:
1. Get the number of each time level (year, month, day, hour, minute, second) in the new date () Type:
function Date () {
var date = new Date ();
var year = Date.getfullyear ();
var month = Date.getmonth () +1; JS starting from 0 to take
var date1 = Date.getdate ();
var hour = date.gethours ();
var minutes = date.getminutes ();
var second = Date.getseconds ();
Alert (date+ "| "+year+" Year "+month+" month "+date1+" Day "+hour+" when "+minutes +" sub "+second+" Seconds ");
}
The results are: Mon June 15:44:50 utc+0800 2009 | June 15, 2009 15:44 50 seconds
Note: The Date.getmonth () Gets a month that starts at 0, and all the get methods of date except date.getfullyear () gets 2009, the rest of the Get method gets a number less than 10, is in the singular appear, such as June, date.getmonth () +1 = 6 to get two bits of yourself plus 0 on it, as in the following conversion method.
The following is a conversion to another two format, and the above is similar:
(1)
Convert the date type to Tring
The datetime is: Wed Mar 11:05:05 gmt+0800 format results: 2009-06-12 17:18:05
function Datetostr (datetime) {
var year = Datetime.getfullyear ();
var month = Datetime.getmonth () +1;//JS starting from 0
var date = Datetime.getdate ();
var hour = datetime.gethours ();
var minutes = datetime.getminutes ();
var second = Datetime.getseconds ();
if (month<10) {
month = "0" + month;
}
if (date<10) {
Date = "0" + date;
}
if (hour <10) {
hour = "0" + hour;
}
if (minutes <10) {
minutes = "0" + minutes;
}
if (second <10) {
Second = "0" + second;
}
var time = year+ "-" +month+ "-" +date+ "" +hour+ ":" +minutes+ ":" +second; 2009-06-12 17:18:05
alert (time);
return time;
}
(2)
Convert the date type to Tring
The datetime is: Wed Mar 11:05:05 gmt+0800 format results: June 12, 09 17:18
function Datetocomm (datetime) {
var year = Datetime.getfullyear ();
var month = Datetime.getmonth () +1;//JS starting from 0 to take
var date = Datetime.getdate ();
var hour = datetime.gethours ();
var minutes = datetime.getminutes ();
var second = Datetime.getseconds ();
if (month<10) {
month = "0" + month;
}
if (date<10) {
Date = "0" + date;
}
if (hour <10) {
hour = "0" + hour;
}
if (minutes <10) {
minutes = "0" + minutes;
}
if (second <10) {
Second = "0" + second;
}
Year = year.tostring ();
Year = year.substring (2);
var time1 = year+ "year" +month+ "month" +date+ "Day";
var time2 = hour+ "When" +minutes+ "minute";//June 12, 09 17:18
var time={time1:time1,time2:time2}//JSON format
alert (time);
return time;
}
2, the actual application often will encounter the situation of adding and reducing time, such as: Yesterday this time to today this time between the system has landed several times and so on. So we'll subtract a day or a few days after we get the current time.
Here we will now get the time to convert under:
function Date () {
var date = new Date ();
var datenum date.gettime ();
alert (datenum);
}
The resulting is: 2592000000 This is the number of milliseconds from January 1, 1970.
Note: the GetTime () method can return the number of milliseconds from January 1, 1970.
We take the day as an example, get a day ago at this time:
function Date () {
var date = new Date ();
var onedate = 1
var datemid = Date.gettime ()-(onedate * 24 * 60 * 60 * 1000)
var datebefore = new Date (DATEMID);
alert (Datebefore);
}
If date is: Mon June 15:44:50 utc+0800 2009
Then output: Mon June 15:44:50 utc+0800 2009
To convert to sreing type, follow the above 1 method to convert it.
GetTime () method Tutorial: http://www.w3school.com.cn/js/jsref_getTime.asp