开发者

Javascript/jQuery - DateTime to Date and Time separate strings

开发者 https://www.devze.com 2023-04-08 03:50 出处:网络
Is there any simple 开发者_开发百科way to convert the following: 2011-08-31T20:01:32.000Z In to UK date format: 31-08-2011

Is there any simple 开发者_开发百科way to convert the following:

2011-08-31T20:01:32.000Z

In to UK date format: 31-08-2011

and time to: 20:01


You can use momentjs (http://momentjs.com/):

var date = moment(dateObject).format("YYYY-MM-DD");
var time = moment(dateObject).format("HH:mm:ss"); 


You can use jquery-dateFormat plugin. The following should do the trick:

$.format.date('2011-08-31T20:01:32.000Z', "dd-MM-yyyy"));
$.format.date('2011-08-31T20:01:32.000Z', "hh:mm"));


Date:

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var date = currentTime.getDate();
var year = currentTime.getFullYear();
$('#date1').html(date + '-' + month + '-' + year);

Time:

<script type="text/javascript">
  var tick;

  function stop() {
    clearTimeout(tick);
  }

  function clock() {
    var ut=new Date();
    var h,m,s;
    var time="";
    h=ut.getHours();
    m=ut.getMinutes();
    s=ut.getSeconds();
    if(s<=9) s="0"+s;
    if(m<=9) m="0"+m;
    if(h<=9) h="0"+h;
    time+=h+":"+m+":"+s;
    document.getElementById('clock').innerHTML=time;
    tick=setTimeout("clock()",1000); 
  }
</script>
<body onload="clock();" onunload="stop();"> 
  <p><span id="clock"></span></p>
</body>


var a = '2011-08-31T20:01:32.000Z';
var b = new Date(a);

See http://www.w3schools.com/jsref/jsref_obj_date.asp for methods you can use on b now.


var rg=/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\..*/g;
var dateStr="2011-08-31T20:01:32.000Z".replace(rg,"$3-$2-$1"); // result is 31-08-2011
var timeStr="2011-08-31T20:01:32.000Z".replace(rg,"$4:$5"); // result is 20:01


var date = new Date("2011-08-31T20:01:32.000Z").toLocaleDateString();
// date = 2011/08/31

date = date.split("/");
// date = ["31", "08, "2011"]    

date = date[2] + "-" + (date[0].length == 1 ? "0" + date[0] : date[0]) + "-" + (date[1].length == 1 ? "0" + date[1] : date[1]);
// data = 2011-31-08

$("#your-txtbox").val(date);


Use the date object:

d = new Date('2011-08-31T20:01:32.000Z');
date = d.format("dd-mm-yyyy");
time = d.format("HH:MM");
0

精彩评论

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