0

How can i get

Year Month Date Seconds ??

So that it looks like this

14062154

When tried it this way

function myFunction() {


var dt = new Date();

alert(dt.getYear() +  dt.getMonth() + 1  + dt.getDate()+ dt.getSeconds());
}

The result was 201 Could anybody please let me kniw how can i make correction ??

Pawan
  • 31,545
  • 102
  • 256
  • 434

4 Answers4

2

All of those function return numbers, and what happens when you put a + sign between numbers - it adds them.

Try turning them into strings first so they are concatenated, not added

i.e.

function myFunction() {    
var dt = new Date();    
alert(dt.getYear().toString() +  (dt.getMonth() + 1).toString()  + dt.getDate().toString() + dt.getSeconds().toString());
}

A couple of other points as per comments

.getYear() is old and shouldn't really be used (see SO question here), it returns 114 for 2014, you should use .getFullYear(), this will return 2014, but since you may only want a two digit year, trim the first two characters off

Something like:

function myFunction() {    
var dt = new Date();    
alert(dt.getYear().toString().substr(2) +  (dt.getMonth() + 1).toString()  + dt.getDate().toString() + dt.getSeconds().toString());
}

Assuming you are tring to keep a set format of 2 digits for year, 2 digits for month, 2 digits for day and 2 digits for seconds, if any of these are below 10, you will need to pad with a zero. So I would make a helper function to pad,

Something like:

function pad(s, size) {    
    while (s.length < size) s = "0" + s;
    return s;
}

then you can add it to your function

Something like:

function myFunction() {    
var dt = new Date();    
alert(pad(dt.getYear().toString().substr(2),2) +  pad((dt.getMonth() + 1).toString(),2)  + pad(dt.getDate().toString(),2) + pad(dt.getSeconds().toString(),2));
}
Community
  • 1
  • 1
OJay
  • 4,763
  • 3
  • 26
  • 47
  • The output of the above code is not exactly the same format as the above example you've given. You might consider this one: http://jsfiddle.net/TvMLE/ :)) – dunli Jun 23 '14 at 00:58
  • if the month, date and seconds are in single digit, then it would come as 114654 where 114 is getYear, 6 is month, 5 is date and 4 is seconds – Bharath Jun 23 '14 at 01:06
0

Hi try this,

function addZeros(val)
{
    val    = ""+val;
    return val.length === 1 ? "0"+val : val;
}

function myFunction() 
{

 var dt = new Date();

 alert(dt.getUTCFullYear().toString().slice(2)+ addZeros(dt.getMonth()+1)+addZeros(dt.getDate())+ addZeros(dt.getSeconds()));
}
Bharath
  • 519
  • 4
  • 7
0

Are you trying to use a timestamp? Javascript natively uses a timestamp in milliseconds, this could result in less code.

var timestamp_milliseconds = new Date().getTime();

if you want the timestamp to be in seconds, just divid by 1000;

var timestamp_seconds = new Date().getTime() / 1000;
Michael
  • 6,895
  • 3
  • 23
  • 19
0

You could also build an array and then join it, this is a little less code.

function getTimestamp() {

  var dt = new Date();


  return [dt.getYear(), dt.getMonth() + 1, dt.getDate(), dt.getSeconds()].join('');

}
Michael
  • 6,895
  • 3
  • 23
  • 19