在jquery中从datetime获取时间



如何从jquery的datetime中获取时间

,

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

我只需要11:30:00从这个

time = 11:30:00

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

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;

尝试获取当前日期的当前时间

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

这是一个解决方案,

创建一个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;

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

何不利用这些绝佳的时刻呢

使用JavaScript很容易做到。

使用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"
*/

相关内容

  • 没有找到相关文章

最新更新