jQuery 从jquery中的日期时间获取时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15245894/
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-08-26 14:39:32  来源:igfitidea点击:

Get time from datetime in jquery

jquerydatetime

提问by Developer

How to get time form the datetime in jquery

如何从jquery中的日期时间获取时间

Suppose,

认为,

datetime = Wed Mar 06 2013 11:30:00 GMT+0530 (IST)

I need only 11:30:00 from this such that

我只需要 11:30:00 这样

time = 11:30:00

回答by u635504

check out the W3C Javascript Datetime Object Reference:

查看W3C Javascript 日期时间对象参考

var hours = datetime.getHours(); //returns 0-23
var minutes = datetime.getMinutes(); //returns 0-59
var seconds = datetime.getSeconds(); //returns 0-59

your question:

你的问题:

if(minutes<10)
  minutesString = 0+minutes+""; //+""casts minutes to a string.
else
  minutesString = minutes;

回答by Stuart Eske

Here is a solution,

这里有一个解决方案,

Create a Javascript Date:

创建一个 Javascript 日期:

var aDate = new Date(
    Date.parse('Wed Mar 06 2013 11:30:00 GMT+0530 (IST)');
);

Build the output string:

构建输出字符串:

var dateString = '';

var h = aDate.getHours();
var m = aDate.getMinutes();
var s = aDate.getSeconds();

if (h < 10) h = '0' + h;
if (m < 10) m = '0' + m;
if (s < 10) s = '0' + s;

dateString = h + ':' + m + ':' + s;

Documentation Links: http://www.w3schools.com/jsref/jsref_obj_date.asp

文档链接:http: //www.w3schools.com/jsref/jsref_obj_date.asp

回答by Swarne27

Try this to get the current day's current time

试试这个来获取当天的当前时间

$(document).ready(function() {
    var today = new Date();
    var cHour = today.getHours();
    var cMin = today.getMinutes();
    var cSec = today.getSeconds();
    alert(cHour+ ":" + cMin+ ":" +cSec );
});

回答by adrian

What about using the excellent momentjs

使用优秀的momentjs怎么样