简体   繁体   中英

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 be 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.

由于dayClick已经为您提供了Javascript Date对象,因此您可以使用Mozilla网站中的此函数将其格式化为8601格式的日期字符串

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 '';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM