Javascript Converting IETF Date to ISO8601 Format
I am using this awesome jQuery calendar plugin
http://arshaw.com/fullcalendar/
One of the options for clicking on a date is a callback function to return the date that is clicked on.
http://arshaw.com/fullcalendar/docs/mouse/dayClick/
I believe it only returns the date in this format IETF format - Wed, 18 Oct 2009 13:00:00 EST
I however, need it to 开发者_如何学JAVAbe in ISO861 format to post the data. I can't seem to find anything on it in google. I am trying to convert it in Javascript. If not, then conversion can take place in the Java backend. Help is appreciated
From the fine manual:
date
holds a Date object for the current day.
Emphasis mine.
I'd guess that the IETF format you're seeing is just the default stringification of the Date
object. The Date class has getYear()
, getMonth()
, and getDate()
methods so you can easily produce an ISO-8601 date string if you're careful with the zero-padding of course.
Since dayClick gives you a Javascript Date object already, you can format it as an 8601 formatted date string using this function from the Mozilla site.
it returns a regular javascript date object. you can use the fullcalendar formatDate
utility function with it like this:
$.fullCalendar.formatDate(date, 'u');
full docs for formatDate
you can use the formatDate function belong on your Date object dayClick!
function getGMTOffset(localDate) {
var positive = (localDate.getTimezoneOffset() > 0);
var aoff = Math.abs(localDate.getTimezoneOffset());
var hours = Math.floor(aoff / 60);
var mins = aoff % 60;
var offsetTz = padzero_(hours) + ':' + padzero_(mins);
// getTimezoneOffset() method returns difference between (GMT) and local time, in minutes.
// example, If your time zone is GMT+2, -120 will be returned.
// This is why we are inverting sign
if (!positive) {
return '+' + offsetTz;
}
return '-' + offsetTz;
}
function pad2zeros(n) {
if (n < 100) {
n = '0' + n;
}
if (n < 10) {
n = '0' + n;
}
return n;
}
function padzero(n) {
return n < 10 ? '0' + n : n.toString();
}
function formatDate(date) {
if (date) {
return (date.getFullYear()) +
'-' + padzero((date.getMonth() + 1)) +
'-' + padzero(date.getDate()) +
'T' + padzero(date.getHours()) +
':' + padzero(date.getMinutes()) +
':' + padzero(date.getSeconds()) +
'.' + pad2zeros(date.getMilliseconds()) +
getGMTOffset(date);
}
return '';
}
精彩评论