-1

I have a date string CStartDate as '3/23/2014' that I am getting from siebel side.I need to convert it to UTC time zone where timeoffset becomes zero.I have tried something like this:

var a = CStartDate.split('/');

var c = a[0];
a[0] = a[1];
a[1] = c; 

var tacStartDate = new Date(a[2],parseInt(a[1], 10) - 1,a[0]);
alert(tacStartDate);

This alert returns as 'Sun Mar 23 2014 00:00:00 GMT+0530 (India Standard Time)', but I don't want that offset GMT+0530 (India Standard Time), rather I want it only to be 'Sun Mar 23 2014 00:00:00 GMT+0000'.I want this as a date object which will be indicating date of GMT, not any other location. How can I achieve that?

Thanks in advance.

Chiranjit
  • 53
  • 1
  • 8
  • 1
    I would reccommend [moment.js](http://momentjs.com/) for this. Makes life a whole lot easier. – Liam Mar 26 '14 at 09:21

2 Answers2

1

try toISOString() on the Date object. If using less than ECMAScript 5, there is a polyfill for the function

if ( !Date.prototype.toISOString ) {
  ( function() {

    function pad(number) {
      if ( number < 10 ) {
        return '0' + number;
      }
      return number;
    }

    Date.prototype.toISOString = function() {
      return this.getUTCFullYear() +
        '-' + pad( this.getUTCMonth() + 1 ) +
        '-' + pad( this.getUTCDate() ) +
        'T' + pad( this.getUTCHours() ) +
        ':' + pad( this.getUTCMinutes() ) +
        ':' + pad( this.getUTCSeconds() ) +
        '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice( 2, 5 ) +
        'Z';
    };

  }() );
}
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
0

There is an UTC method of Date. It should be as simple as:

new Date(Date.UTC(a[2],parseInt(a[1], 10) - 1,a[0]));
blue
  • 1,939
  • 1
  • 11
  • 8
  • Hi blue I tried it and it returns as 'Sun Mar 23 2014 05:30:00 GMT+0530 (India Standard Time)'. but my concern is its anyway producing the local time which I don't want. I want an object which will give me date lets say if I am staying at GMT, then it will produce something like 'Sun Mar 23 2014 00:00:00 GMT+0000 (GMT)'.I want this time object.In the requirement I can't change the date location, so if i use above method it will produce local time not the GMT.How can i achieve that? – Chiranjit Mar 26 '14 at 09:45
  • Take a look at the answers to [Convert date to another timezone in javascript](http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – blue Mar 26 '14 at 09:57