0

我正在使用 Ajax 标头来获取当前时间,我怎样才能只检索小时、分钟和秒并将它们从这个字符串中存储在它们自己的变量中:

Wed, 15 May 2013 18:05:32 GMT

谢谢!

4

2 回答 2

2

从标题中读取日期并将其传递给 Date 对象

var headerDate = "Wed, 15 May 2013 18:05:32 GMT";
    var date = new Date(headerDate );
    var hour = date.getHours();
    var min = date.getMinutes();
    var secs = date.getSeconds();
于 2013-05-15T18:10:48.480 回答
1

只是为了详细说明@mclaassen 的答案:

//your date string
var dateStr  = 'Wed, 15 May 2013 18:05:32 GMT';
//regex patter to search on 
var patt  = /\d{2}:\d{2}:\d{2}/;
//return the matching date string
var result = patt.exec(dateStr);

var hms = result.split(':');
//set individual variables
var hours = hms[0], min = hms[1], sec = hms [2];
于 2013-05-15T18:19:24.267 回答