javascript - 创建一个链接以将事件添加到 yahoo calandar
问题描述
我要生成一个链接以将事件添加到雅虎日历。我正在关注此文档。我的脚本看起来像这样。
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);
我的开始时间是这样的,6/12/2020 11:06:00 AM
. 生成链接是这样的。持续时间是正确的。但是开始时间不对。我很困惑
st
参数并使用时区。
解决方案
我认为您需要的是这个,因为&st=
当您已经将正确的开始日期存储在变量中时,您将字符串传递给查询参数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(''));
并回答您关于st
. 如果您查看这篇文章 - Date.prototype.getTimezoneOffset()那里它说该方法返回时区差异,以分钟为单位,从当前区域设置(主机系统设置)到 UTC。
如果您从这篇文章中检查第二个答案Parse date without timezone javascript
getTimeOffset()
减法后返回的分钟数与存储的毫秒数相乘MS_IN_MINUTES
对于您的日期在全球范围内为所有用户正确工作是必要的。
推荐阅读
- jupyter-notebook - ipython 笔记本中的目录
- r - 使用子图循环遍历 R Plotly 并隐藏除一个之外的所有图例
- angular - 如何从 ngx markdown 编辑器中去掉预览图标?
- tatsu - TatSu:如何优化以下语法逻辑以获得更快的解析时间?
- php - 在wordpress中上传之前获取图像创建日期
- sql-server - Flyway 模式仅仅是提供信息还是影响功能?
- regex - 正则表达式满足密码字段的 4 个条件中的 3 个
- spring-boot - 多个@OneToMany实体和内部实体本地化的Spring数据JPA派生查询
- php - 在 foreach PHP Codeigniter 中调用存储过程
- javascript - 如何在提交时显示图像