user198729
user198729

Reputation: 63646

How to get current time in the format 2009-12-24 14:20:57 in javascript?

I'm not familiar with time operations in javascript.

I tried new Date(); which gives the result in wrong format:

Thu Dec 24 2009 14:24:06 GMT+0800

How to get the time in format of 2009-12-24 14:20:57

Upvotes: 6

Views: 16879

Answers (7)

tamazi aladashvili
tamazi aladashvili

Reputation: 11

to format time you can do

prints : 8:32:33

Share Edit Follow edited Dec 24 '09 at 6:29 answered Dec 24 '09 at 6:24

EzzDev

thx EzzDev <33333 in my case:

<input type="time" id="appt">
<input type="button" value="click" onclick="gettimenow()">
<script>
function gettimenow(){
       
            var d = new Date();
        var curr_hour = d.getHours()<10? '0'+d.getHours():d.getHours();
        var curr_min = d.getMinutes()<10? '0'+d.getMinutes():d.getMinutes();
            var time24 =  curr_hour + ":" + curr_min;
        document.getElementById('appt').value = time24;
        console.log(time24);
        }
</script>  

Upvotes: 1

Tim Down
Tim Down

Reputation: 324597

My JavaScript implementation of Java's SimpleDateFormat's format method may help you: http://www.timdown.co.uk/code/simpledateformat.php

Upvotes: 0

Chandra Patni
Chandra Patni

Reputation: 17587

<script type="text/javascript">
  function formatDate(d){
   function pad(n){return n<10 ? '0'+n : n}
   return [d.getUTCFullYear(),'-',
          pad(d.getUTCMonth()+1),'-',
          pad(d.getUTCDate()),' ',
          pad(d.getUTCHours()),':',
          pad(d.getUTCMinutes()),':',
          pad(d.getUTCSeconds())].join("");
  }

  var d = new Date();
  var formattedDate = formatDate(d); 
</script

Upvotes: 3

Rob Van Dam
Rob Van Dam

Reputation: 7960

There is no cross browser Date.format() method currently. Some toolkits like Ext have one but not all (I'm pretty sure jQuery does not). If you need flexibility, you can find several such methods available on the web. If you expect to always use the same format then:

var now = new Date();
var pretty = [
  now.getFullYear(),
  '-',
  now.getMonth() + 1,
  '-',
  now.getDate(),
  ' ',
  now.getHours(),
  ':',
  now.getMinutes(),
  ':',
  now.getSeconds()
].join('');

Upvotes: 13

rahul
rahul

Reputation: 187060

Here is a nice on

JavaScript (ActionScript) Date.format

or you can use like this

var dt = new Date();

var timeString = dt.getFullYear() +  "-" + dt.getMonth() + "-" + dt.getDate() + " " + dt.getHours() + ":" + dt.getMinutes() +":" + dt.getSeconds()

Upvotes: 1

EzzDev
EzzDev

Reputation: 10220

to format date

<script type="text/javascript">
<!--
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year);
//-->
</script>

prints : 24-12-2009

to format time you can do

<script type="text/javascript">
<!--

var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();

var curr_sec = d.getSeconds();

document.write(curr_hour + ":" + curr_min + ":" 
+ curr_sec);

//-->
</script>

prints : 8:32:33

Upvotes: 2

Myles
Myles

Reputation: 21510

Use the pieces of the date as found here, to format to your liking.

ie.

var now = new Date();
var formatStr = now.getFullYear() + " - " + now.getMonth() + " - " + now.getDate();
// formatStr = "2009 - 12 - 23"

Upvotes: 0

Related Questions