简体   繁体   中英

Google calendar API Date and time formatting

I am working on a Infoscreen that shows information from a company calendar, but google calendar api gives me dates in toISOString format which looks like: 2015-12-02T14:15:00.000+05:00, how can i reformat it to for example: 12.02.15 14:15?

So it would work together with this code:

function listUpcomingEvents() {
    var request = gapi.client.calendar.events.list({
      'calendarId': 'MyID',
      'timeMin': (new Date()).toISOString(), //If i change format here code doesn't work
      'showDeleted': false,
      'singleEvents': true,
      'maxResults': 1,
      'orderBy': 'startTime'
    });

    request.execute(function(resp) {
      var events = resp.items;
      appendPre('');


      if (events.length > 0) {
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          var when = event.start.dateTime;
          if (!when) {
            when = event.start.date;
          }
          var when2 = event.end.dateTime;
          if (!when2) {
            when2 = event.end.date;
          }
          appendPre(' Fra ' + when + ' Til ' + when2 + ' ' + event.summary)
        }
      } else {
        appendPre('No upcoming events found.');
      }

    });
  }

I finally managed to get it to work, here is how i did it:

if (events.length > 0) {
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          var when = event.start.dateTime;
          if (!when) {
            when = event.start.date;
          }
          var when2 = event.end.dateTime;
          if (!when2) {
            when2 = event.end.date;
          }
        var d = new Date(when);
                var time = d.toLocaleString();
            var d = new Date(when2);
                var time2 = d.toLocaleString();

          appendPre(' Fra ' + time + ' Til ' + time2 + ' ' + event.summary)

Use moment.js library ( https://momentjs.com/ )

const googleApiDate = '2015-12-02T14:15:00.000+05:00';
const formattedDate = moment(googleApiDate).format('DD.MM.YY HH:mm'); // '12.02.15 14:15'

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