如何从jquery中的日期时间中获取时间
假设,
datetime = Wed Mar 06 2013 11:30:00 GMT+0530 (IST)
我只需要11:30:00这样
time = 11:30:00
答案 0 :(得分:8)
查看W3C Javascript Datetime Object Reference:
var hours = datetime.getHours(); //returns 0-23
var minutes = datetime.getMinutes(); //returns 0-59
var seconds = datetime.getSeconds(); //returns 0-59
你的问题:
if(minutes<10)
minutesString = 0+minutes+""; //+""casts minutes to a string.
else
minutesString = minutes;
答案 1 :(得分:4)
这是一个解决方案,
创建Javascript日期:
var aDate = new Date(
Date.parse('Wed Mar 06 2013 11:30:00 GMT+0530 (IST)');
);
构建输出字符串:
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;
答案 2 :(得分:3)
尝试此操作以获取当天的当前时间
$(document).ready(function() {
var today = new Date();
var cHour = today.getHours();
var cMin = today.getMinutes();
var cSec = today.getSeconds();
alert(cHour+ ":" + cMin+ ":" +cSec );
});
答案 3 :(得分:1)
如何使用优秀的momentjs
答案 4 :(得分:0)
使用
Date.prototype.toLocaleTimeString
var date = new Date();
console.log(date.toLocaleTimeString('pt-BR'));
// Output: "20:30:39"
有些语言环境会给你不同的结果,这就是为什么我使用 pt-BR
作为格式(24 小时没有 AM/PM)(时区将保持不变,这只会改变格式)。
您还可以使用任何其他语言环境并传递 {hour12:false}
作为选项。
const event = new Date('August 19, 1975 23:15:30 GMT+00:00');
var options = {hour12:false}
console.log("en-US (options): " + event.toLocaleTimeString('en-US', options));
console.log("en-US : " + event.toLocaleTimeString('en-US'));
console.log("ar-EG (options): " + event.toLocaleTimeString('ar-EG', options));
console.log("ar-EG : " + event.toLocaleTimeString('ar-EG'));
console.log("pt-BR (options): " + event.toLocaleTimeString('pt-BR', options));
console.log("pt-BR : " + event.toLocaleTimeString('pt-BR'));
/* Output:
"en-US (options): 20:15:30"
"en-US : 8:15:30 PM"
"ar-EG (options): ٢٠:١٥:٣٠"
"ar-EG : ٨:١٥:٣٠ م"
"pt-BR (options): 20:15:30"
"pt-BR : 20:15:30"
*/