0

I have give the time format as following two format:

new Date("2015-05-05T08:00:00-0400")

while processing the above two code I am getting the

Tue May 05 2015 17:30:00 GMT+0530 (India Standard Time)

I am unable to get the format which I given. I want to get that format(2015-05-05T08:00:00-0400) while processing.

Please help me...

Zee
  • 8,420
  • 5
  • 36
  • 58
user2877778
  • 37
  • 1
  • 8
  • You're going to have to roll your own format function or use a lib like momentjs – Mike Cheel Jun 05 '15 at 12:50
  • 1
    possible duplicate of [How to ISO 8601 format a Date with Timezone Offset in JavaScript?](http://stackoverflow.com/questions/17415579/how-to-iso-8601-format-a-date-with-timezone-offset-in-javascript) – Qantas 94 Heavy Jun 05 '15 at 12:50

3 Answers3

0

Try this just pass some parameter's to this function and get the local standard time zone for each country.

function getLocalTimeFromGMT(sTime){
  var dte = new Date(sTime); 
  dte.setTime(dte.getTime() - dte.getTimezoneOffset()*60*1000);       
  dte.toLocaleString();
  var m_names = new Array("January", "February", "March", 
 "April", "May", "June", "July", "August", "September", 
 "October", "November", "December");
 var curr_date = dte.getDate();
 var curr_month = dte.getMonth();
 var curr_year = dte.getFullYear();
 var hour = dte.getHours();
 var min = dte.getMinutes();
 var dn = 'am';
 if(hour==0){ hour = 12; }
 if(hour>=12){
  hour = hour-12;
  dn = 'pm';
 }
 return m_names[curr_month] + " " +  curr_date +" "+hour+":"+min+" "+dn ;

}
Hassan Raza
  • 671
  • 10
  • 27
0

You could break apart the date and perform your own calculations to figure out the timezone offset in hours.

function getCustomIsoFormat(date) {
  // Date
  var y = date.getFullYear();
  var m = ('00' + (date.getMonth() + 1)).substr(-2);
  var d = ('00' + date.getDate()).substr(-2);
  // Time
  var h = ('00' + date.getHours()).substr(-2);
  var i = ('00' + date.getMinutes()).substr(-2);
  var s = ('00' + date.getSeconds()).substr(-2);
  // Offset
  var tz = date.getTimezoneOffset();
  var sign = tz > 0 ? '-' : '+';
  var tzAbs = Math.abs(tz);
  var tzH = ('00' + ~~(tzAbs / 60)).substr(-2);
  var tzM = ('00' + ~~(tzAbs % 60)).substr(-2);
  var off = sign + tzH + tzM;
  
  return y + '-' + m + '-' + d + 'T' + h + ':' + i + ':' + s + off;
}

// Input: Tue May 05 2015 17:30:00 GMT+0530 (India Standard Time)
var inputDate = new Date("2015-05-05T08:00:00-0400");

// Expected output: 2015-05-05T08:00:00-0400
document.body.innerHTML = getCustomIsoFormat(inputDate);
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

As noted elsewhere, there is only one standardised output from the javascript Date object and that's in ES5's toISOString, but the only timezone supported is UTC (Z).

A function to return an ISO 8601 formatted string in the local timezone is below.

/*  Return a string in ISO 8601 format with current timezone offset
**
**  e.g. 2014-10-02T23:31:03+0800
**
**  Default is the current date.
**
**  @param {Date} d - Date object to create string from
**  @returns {string} - ISO 8601 format string with local timezone offset
*/
function toLocalISOString(d) {

   d = d || new Date();

  // Pad to two digits with leading zeros
  function pad(n){
    return (n<10?'0':'') + n;
  }

  // Pad to three digits with leading zeros
  function padd(n){
    return (n<100? '0' : '') + pad(n);
  }

  // Convert offset in mintues to +/-HHMM
  // Note change of sign
  // e.g. -600 => +1000, +330 => -0530
  function minsToHHMM(n){
    var sign = n<0? '-' : '+';
    n = Math.abs(n);
    var hh = pad(n/60 |0);
    var mm = pad(n%60);
    return  sign + hh + mm;
  }

  var offset = minsToHHMM(d.getTimezoneOffset() * -1);

  return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) +
         'T' + pad(d.getHours()) + ':' + pad(d.getMinutes()) + ':' + pad(d.getSeconds()) +
         '.' + padd(d.getMilliseconds()) + offset;
}
RobG
  • 142,382
  • 31
  • 172
  • 209