开发者

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


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('');


<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


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


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()


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"


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


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>  

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜