0

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

Udders
  • 67
  • 2
  • 9
  • 1
    Have you tried moment.js? It is a small library for managing dates in javascript. Really easy to implement in that. http://momentjs.com/ – gregpakes Jul 31 '13 at 14:22
  • If you're able to use libraries I would check out http://momentjs.com/ to help you deal with Date/Time in JS. – Stephen Gilboy Jul 31 '13 at 14:22

4 Answers4

2

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();
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

Firstly, you need to deal with the fact that month is 0 based (i.e. 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 (e.g. 31). getDay() will return you the weekday (e.g. 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!

musefan
  • 47,875
  • 21
  • 135
  • 185
0

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);
CAMPSMITH
  • 25
  • 5
-2

I know the OP asked about Javascript, but specifying Javascript was the wrong question, judging by the ugly (though correct) answers.
HTML doesn't have a way of printing the current date and time, and it should have. Someone should invent one.
But PHP is the way to do what is wanted:

<?php print date("j M Y H:i"); ?>  

PHP has 38 different format symbols covering every need, see https://www.php.net/manual/en/function.date.php

  • HTML is markup and it should not have to. It looks like the help was needed for the front side, not for the back. There a wide variety of solutions to have it done on the front. But yours would work on the back as well. – Damask Oct 05 '19 at 05:52