简体   繁体   中英

javascript output date in correct format

I am wanting to output the Date with javascript in a specific format I am wanting to output in

2013-7-31 15:17:56

How would I go about doing this, I have googled around, and found functions like,

getYear() & getMonth() & getDay()

But I seem to get this output with those functions

2013-6-3

There are a few methods that you can use to handle this Date object as seen below :

Creating a Prototype Function

You could create a very basic prototype function which would allow you to explicitly build a string using each of the components, which may be an excellent approach if you intend to reuse this function or similar ones often :

//Creating a Prototype 
Date.prototype.yyyymmddhhmmss = function() {
   //Grab each of your components
   var yyyy = this.getFullYear().toString();
   var MM = (this.getMonth()+1).toString();
   var dd  = this.getDate().toString();
   var hh = this.getHours().toString();
   var mm = this.getMinutes().toString();
   var ss = this.getSeconds().toString();

   //Returns your formatted result
  return yyyy + '-' + (MM[1]?MM:"0"+MM[0]) + '-' + (dd[1]?dd:"0"+dd[0]) + ' ' + (hh[1]?hh:"0"+hh[0]) + ':' + (mm[1]?mm:"0"+mm[0]) + ':' + (ss[1]?ss:"0"+ss[0]);
};

//Usage
alert(new Date().yyyymmddhhmmss());

Example

Output as yyyy-mm-dd

Very similar to the example above, you can directly build the string using the individual components of the Date object :

 //Create a Date object
 var date = new Date();

 //Concatenate the sections of your Date into a string ("yyyy-mm-dd")
 var formatted = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

Output as yyyy-mm-dd hh:mm:ss

This method is identical to the above with the exception that it also includes a few additional fields such as hours, minutes and seconds.

 var date = new Date();

 var formatted = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();

Firstly, you need to deal with the fact that month is 0 based (ie 0 = Jan, 1 = feb, etc.). For that you can simple increment the month value, when you get the 3 values:

var year = x.getFullYear();
var month = x.getMonth() + 1;
var day = x.getDate();

NOTE : You need to use getDate() for the day part (eg 31 ). getDay() will return you the weekday (eg 3 = Wednesday)

Secondly, you need to deal with the single digit display. For that you can check if the value is less than 10 and if so, pad it with a "0". A little helper function can do this for you:

function pad(x){
    if(x < 10) return "0" + x;
    return x;
}

Which you can finally use like so:

var myDate = year + "-" + pad(month) + "-" + pad(day);

Here is a working example

Of course, as others have said, using a library would be much easier!

Here's a simple approach that works for me. It can be extended for all Date parameters.

var mm = currentDay.getMonth()+1;
mm = (mm<10?"0"+mm:mm);
var dd = currentDay.getDate();
dd = (dd<10?"0"+dd:dd);

$log.info(mm+"/"+dd);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM