0

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

    });
  }
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Linda Lawton - DaImTo Dec 19 '16 at 09:22
  • No since it is using google api dateTime which i dont get to format the same way... – Nikita Sumahers Dec 19 '16 at 11:41

2 Answers2

0

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)
0

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'