0

I am trying to get the date using html5 date type,and it is used for an api call with some parameters,but i will get the following error message from api.

date field must be yyyy-mm-ddTH:i:s+Z

I wll read the date as this format

Sun Jun 12 2016 00:00:00 GMT+0530 (India Standard Time)

So how can i convert the above date into yyyy-mm-ddTH:i:s+Z format?

Note: I am using javascript

UPDATE

I am used toISOString() method but it didn't worked

Blessan Kurien
  • 1,645
  • 9
  • 31
  • 63

2 Answers2

1

It's very easy to use momentjs...

Please, check my fiddle

var time = 'Sun Jun 12 2016 00:00:00 GMT+0530';

time = moment(time).format("YYYY-MM-DDThh:mm:ssZ");

$('.time').text(time);

Hope this helps!

0

I hope this is what you wanted:

function getTimeZone(date) {
  var offset = date.getTimezoneOffset(),
    o = Math.abs(offset);
  return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}

function getFormattedDate(date) {
  var day = date.getDate();
  var month = date.getMonth();
  var year = date.getFullYear();
  var hours = date.getHours();
  var mins = date.getMinutes();
  var sec = date.getSeconds();
  var timeZone = getTimeZone(date);

  return year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + sec + ' ' + timeZone;

}


document.write(getFormattedDate(new Date()));

This will print: 2016-5-2 19:54:59 +06:00

JsFiddle link: https://jsfiddle.net/sktajbir/pmbdk33L/2/

FYI, getTimeZone() function is taken from here.

Thanks.

Community
  • 1
  • 1
Sk. Tajbir
  • 290
  • 2
  • 9