Reputation: 1665
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
Upvotes: 0
Views: 297
Reputation: 290
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.
Upvotes: 0
Reputation: 133
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!
Upvotes: 1