1

I am tying to generate a link to add an event to yahoo calendar. I am following this documentation. My script look like this.

  var MS_IN_MINUTES = 60 * 1000;

  var formatTime = function (date) {
     return date.toISOString().replace(/-|:|\.\d+/g, '');
  };

  var calculateEndTime = function (event) {
      return event.end ?
             formatTime(event.end) :
             formatTime(new Date(event.start.getTime() + (event.duration * MS_IN_MINUTES)));
        };

  var yHDuration = (Number(Duration.split(':')[0]) * 60 + Number(Duration.split(':')[1]));

  var event = {
      title: 'Get on the front page of HN',     // Event title
      start: new Date(MeetingTime),   // Event start date
      duration: yHDuration,                            // Event duration (IN MINUTES)
      // If an end time is set, this will take precedence over duration
      address: 'The internet',
      description: 'Get on the front page of HN, then prepare for world domination.',
  }

  var eventDuration = event.end ?
      ((event.end.getTime() - event.start.getTime()) / MS_IN_MINUTES) :event.duration;

  // Yahoo dates are crazy, we need to convert the duration from minutes to hh:mm
  var yahooHourDuration = eventDuration < 600 ?
      '0' + Math.floor((eventDuration / 60)) :
      Math.floor((eventDuration / 60)) + '';

  var yahooMinuteDuration = eventDuration % 60 < 10 ?
      '0' + eventDuration % 60 :
      eventDuration % 60 + '';

   var yahooEventDuration = yahooHourDuration + yahooMinuteDuration;

      // Remove timezone from event time
      var st = formatTime(new Date(event.start - (event.start.getTimezoneOffset() *
      MS_IN_MINUTES))) || '';

   var href = encodeURI([
       'http://calendar.yahoo.com/?v=60&view=d&type=20',
       '&title=' + (event.title || ''),
       '&st=' + '20200611225200',
       '&dur=' + (yahooEventDuration || ''),
       '&desc=' + (event.description || ''),
       '&in_loc=' + (event.address || ''),
        '&invitees=' + (InviteesArr || ''),
    ].join(''));

   var link = '<a class="icon-yahoo" target="_blank" href="' +
       href + '">Yahoo! Calendar</a>';

   console.log(link);

My Start time looks like this, 6/12/2020 11:06:00 AM. the generate link is like this. Duration is correct. But start time is incorrect. I am confused with the

st

parameter and using the timezone.

Vinay
  • 7,442
  • 6
  • 25
  • 48
Brutus
  • 163
  • 1
  • 17

1 Answers1

1

What I think you need is this, because your are passing the string to the &st= query parameter when you already have correct start date stored in variable st:

var href = encodeURI([
       'http://calendar.yahoo.com/?v=60&view=d&type=20',
       '&title=' + (event.title || ''),
       '&st=' + (st || ''),
       '&dur=' + (yahooEventDuration || ''),
       '&desc=' + (event.description || ''),
       '&in_loc=' + (event.address || ''),
        '&invitees=' + (InviteesArr || ''),
    ].join(''));

And to answer your question about st. If you check this article - Date.prototype.getTimezoneOffset() there it says that method returns the time zone difference, in minutes, from current locale (host system settings) to UTC.

And if you check the second answer from this post Parse date without timezone javascript subtraction after multiplying minutes returned with getTimeOffset() with milliseconds stored in MS_IN_MINUTES is necessary for your date to work correctly around the globe for all users.