云函数cron-parser解析时区问题

文章讲述了在云函数中使用cron-parser解析CronExpression遇到的0点变为8点的问题,经调查发现是时区设置问题。解决方法是使用tz选项指定用户期望的时区,如Asia/Shanghai。
摘要由CSDN通过智能技术生成

1、问题

云函数部署后cron-parser解析0点会变成8点 

考虑可能是时区的问题

然后看文档发现果然有问题,云函数环境是utc+0

2、解决

看了半天cron-parser文档发现

Using `Date` as an input can be problematic specially when using the `tz` option. The issue being that, when creating a new `Date` object without
any timezone information, it will be created in the timezone of the system that is running the code. This (most of times) won't be what the user
will be expecting. Using one of the supported `string` formats will solve the issue(see timezone example).

* *iterator* - Return ES6 compatible iterator object 
* *utc* - Enable UTC
* *tz* - Timezone string. It won't be used in case `utc` is enabled

大概就是tz就是设置时区的方法

然后查找时区字符值

中国, 亚洲: 当前本地时间 & 日期、时区和时差

parser.parseExpression(task.do_cron,{tz: 'Asia/Shanghai'});

果然可以 问题解决~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值