Chiranjit
Chiranjit

Reputation: 45

How to Convert Hard Coded date into standard GMT format

I need to convert a hard coded date into a standard GMT format.How can I do this? The date I have is in the following format: var myDate = 'dd|mm|yyyy'; There is no time or day description in the date.Just the 'dd|mm|yyyy' string. Is there a way I can convert it into GMT?

Thanks in advance.

Upvotes: 3

Views: 1595

Answers (2)

sabithpocker
sabithpocker

Reputation: 15566

a = '22/02/2014'.split('/')
d = new Date(a[2],parseInt(a[1], 10) - 1,a[0])
//Sat Feb 22 2014 00:00:00 GMT+0530 (India Standard Time)

Now you have a javascript date object in d

utc = d.getUTCDate() + "/" + (d.getUTCMonth() + 1 ) + "/" + d.getUTCFullYear();
//"21/2/2014" for an accurate conversion to UTC time of day is a must.

If you are in say India, the Javascript Date object will have timeZoneOffset 330. So its not possible to keep a javascript Date object with timezone GMT unless your system time is GMT.

So if you want a Date object for calculation, you can create one with localTimezone and simply suppose it is GMT

pseudoGMT = new Date( Date.parse(d) + d.getTimezoneOffset() * 60 * 1000);
//Fri Feb 21 2014 18:30:00 GMT+0530 (India Standard Time)

If you can explain your high level requirement we might be able to help with some alternate solutions.

Upvotes: 2

Bart
Bart

Reputation: 27205

Use regex matching to extract the data you need:

var myDate = "21|01|2014";
var data = myDate.match(/(\d{2})\|(\d{2})\|(\d{4})/);
var date = new Date(data[3], data[2] - 1, data[1]);

Note that the month is 0-indexed, so january = 0

More on regular expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Upvotes: 1

Related Questions