naresh
naresh

Reputation: 174

How to alert current date in yyyy-mm-dd hrs:min:sec format in javascript?

In my program i need to alert current date with time in specific format(2015-11-18 12:23:00) so i am write like this

       var date = new Date();
       alert(date);

but the result is Wed Nov 18 2015 12:24:28 GMT+0530 (India Standard Time).

and also i am try like this

  <script>
    var d = new Date();
    var c = new Date();

    alert(formatDate(c));
    alert(formatDate(d));

   function formatDate(d)
    {
   var month = d.getMonth();
   var day = d.getDate();
   var hours = d.getHours();
   var minutes = d.getMinutes();
   month = month + 1;

   month = month + "";

   if (month.length == 1)
  { 
    month = "0" + month;
   }

  day = day + "";

  if (day.length == 1)
 {
   day = "0" + day;
 }

  hour = hour + "";
 if (hour.length == 1)
 {
   hour = "0" + hour;
 }

  minute = minute + "";
  if (minute.length == 1)
 {
    minute = "0" + minute;
  }

  return d.getFullYear()+month + '' + day + ''+ hour + '' + minute + '';
    }</script> 

it is also not working properly. how can i do this in javascript and also i need to passed the veriable to database in another php file. please help me how can i do this

Upvotes: 2

Views: 3004

Answers (6)

harry
harry

Reputation: 1007

Use this code:

var sec = d.getSeconds();

return d.getFullYear()+'-'+month + '-' + day + ' '+ hour + ':' + minute + ':'+sec;

instead of:

return d.getFullYear()+month + '' + day + ''+ hour + '' + minute + '';

Upvotes: 0

user2982042
user2982042

Reputation: 110

From the below function you will get all the details you want to show use it according to your need

getMonth() - Returns a zero-based integer (0-11) representing the month of the year.

getDate() - Returns the day of the month (1-31).

getDay() - Returns the day of the week (0-6). 0 is Sunday, 6 is Saturday.

getHours() - Returns the hour of the day (0-23).

getMinutes() - Returns the minute (0-59).

getSeconds() - Returns the second (0-59).

getMilliseconds() - Returns the milliseconds (0-999).

getTimezoneOffset() - Returns the number of minutes between the machine local time and UTC.

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var yyyy = today.getFullYear();
if(dd<10){
    dd='0'+dd
} 
if(mm<10){
    mm='0'+mm
} 
var today = dd+'/'+mm+'/'+yyyy;
document.getElementById("DATE").value = today;

Upvotes: 0

Roli Agrawal
Roli Agrawal

Reputation: 2466

This might be useful

Date.prototype.yyyy = function() {
   var yy = this.getFullYear().toString();
   var mm = (this.getMonth()+1).toString();
   var dd  = this.getDate().toString();
   var hh  = this.getHours().toString();
   var min  = this.getMinutes().toString();
   var ss = this.getSeconds().toString();
   return yy +"-" +(mm[1]?mm:"0"+mm[0]) + "-" +(dd[1]?dd:"0"+dd[0])+" "+(hh[1]?hh:"0"+hh[0])+":"+(min[1]?min:"0"+min[0])+":"+(ss[1]?ss:"0"+ss[0]); 
  };
d = new Date(); 
d.yyyy();

Upvotes: 1

mega6382
mega6382

Reputation: 9396

Please try the following code:

Number.prototype.padLeft = function(base,chr){
var  len = (String(base || 10).length - String(this).length)+1;
return len > 0? new Array(len).join(chr || '0')+this : this;
}

 var d = new Date;
function formatDate(d){

    dformat = [ (d.getMonth()+1).padLeft(),
                d.getDate().padLeft(),
                d.getFullYear()].join('-')+
                ' ' +
              [ d.getHours().padLeft(),
                d.getMinutes().padLeft(),
                d.getSeconds().padLeft()].join(':');
 return dformat;
 }
 alert(formatDate(d));

It will return : 11-18-2015 12:17:02. And to pass the value to a php code check this: How to pass JavaScript variables to PHP?

Upvotes: 2

Deenadhayalan Manoharan
Deenadhayalan Manoharan

Reputation: 5444

Try this..

<script>
    var currentdate = new Date(); 
var datetime = currentdate.getFullYear() + "-"
                + (currentdate.getMonth()+1)  + "-" 
                + currentdate.getDate() + " "  
                + currentdate.getHours() + ":"  
                + currentdate.getMinutes() + ":" 
                + currentdate.getSeconds();
                alert(datetime);
    </script>

Output:2015-11-18 12:46:52

Demo:http://js.do/code/73749

Upvotes: 2

piyush
piyush

Reputation: 554

I think this below code are helpful us.

function getDateTimeFormate () {
  now = new Date();
  year = "" + now.getFullYear();
  month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
  day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; }
  hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; }
  minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
  second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; }
  return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}

To get date time formate like.

alert(getDateTimeFormate());

// example alert message: 2011-05-18 15:20:12

Upvotes: 4

Related Questions