Javascript/jQuery - 日期时间到日期和时间分开的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7565879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:33:42  来源:igfitidea点击:

Javascript/jQuery - DateTime to Date and Time separate strings

javascriptjquery

提问by CLiown

Is there any simple way to convert the following:

有什么简单的方法可以转换以下内容:

2011-08-31T20:01:32.000Z

In to UK date format: 31-08-2011

英国日期格式: 31-08-2011

and time to: 20:01

和时间: 20:01

回答by Bitclaw

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

您可以使用 momentjs ( http://momentjs.com/):

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

回答by alh84001

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

您可以使用jquery-dateFormat 插件。以下应该可以解决问题:

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

回答by Niko

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

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

有关您现在可以在 b 上使用的方法,请参见http://www.w3schools.com/jsref/jsref_obj_date.asp

回答by Michael Schwartz

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>

回答by PiTheNumber

Use the date object:

使用日期对象

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

回答by Andrew D.

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,"--"); // result is 31-08-2011
var timeStr="2011-08-31T20:01:32.000Z".replace(rg,":"); // result is 20:01