Convert date format in JS

Source: Internet
Author: User
Tags date1

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

  1. var formatdate = function (date) {
  2. var y = date.getfullyear ();
  3. var m = date.getmonth () + 1;
  4. m = m < 10?  ' 0 ' + m:m;
  5. var d = date.getdate ();
  6. D = d < 10?  (' 0 ' + D): D;
  7. return y + '-' + M + '-' + D;
  8. };

2.js method return value: 2015-03-19 12:00

  1. var FormatDateTime = function (date) {
  2. var y = date.getfullyear ();
  3. var m = date.getmonth () + 1;
  4. m = m < 10?  (' 0 ' + m): M;
  5. var d = date.getdate ();
  6. D = d < 10?  (' 0 ' + D): D;
  7. var h = date.gethours ();
  8. var minute = date.getminutes ();
  9. minute = Minute < 10?  (' 0 ' + minute): minute;
  10. return y + '-' + M + '-' + d+' +h+': ' +minute;
  11. };
    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

    1. var parserdate =  function  (date)  {  
    2.     
    3.     if  (!isnan (t))  {  
    4.          return new date (date.parse (Date.replace (/-/g ,  
    5.      } else {  
    6.          return new date ();   
    7. &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
    8. };  

Call: Parserdate ("2015-03-19 12::00:00")

Convert date format in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.