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
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);
}
What you get 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 * *)
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.
How to format the date value with JS?
1.js method return Value: 2015-03-19
- var formatdate = function (date) {
- var y = date.getfullyear ();
- var m = date.getmonth () + 1;
- m = m < 10? ' 0 ' + m:m;
- var d = date.getdate ();
- D = d < 10? (' 0 ' + D): D;
- return y + '-' + M + '-' + D;
- };
2.js method return value: 2015-03-19 12:00
- var FormatDateTime = function (date) {
- var y = date.getfullyear ();
- var m = date.getmonth () + 1;
- m = m < 10? (' 0 ' + m): M;
- var d = date.getdate ();
- D = d < 10? (' 0 ' + D): D;
- var h = date.gethours ();
- var minute = date.getminutes ();
- minute = Minute < 10? (' 0 ' + minute): minute;
- return y + '-' + M + '-' + d+' +h+': ' +minute;
- };
Call: FormatDate (Date ()) FormatDate (date ())
How to convert 2015-03-12 12:00 to Standard Time ()?
Thu Mar 12:00:00 gmt+0800 (China Standard Time)
JS method return Value: Thu Mar 12:00:00 gmt+0800 (Chinese Standard Time
- var parserdate = function (date) {
-
- if (!isnan (t)) {
- return new date (date.parse (Date.replace (/-/g ,
- } else {
- return new date ();
- &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
- };
Call: Parserdate ("2015-03-19 12::00:00")
Convert date format in JS