user1937021
user1937021

Reputation: 10791

Extract Hours, minutes and seconds from Date format

I am using Ajax headers to get the current time, how can I retrieve just the hours, minutes and seconds and store them in their own variables from this string:

Wed, 15 May 2013 18:05:32 GMT

Thanks!

Upvotes: 0

Views: 3231

Answers (2)

theWizardsBaker
theWizardsBaker

Reputation: 104

Just to elaborate on @mclaassen 's answer:

//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];

Upvotes: 1

Anoop
Anoop

Reputation: 23208

Read date from header and pass that to Date object

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();

Upvotes: 2

Related Questions