简体   繁体   English

从开始dateTime(ISO格式)和持续时间(hh:mm格式)计算结束日期时间(ISO格式)

[英]Calculate end dateTime(ISO format) from start dateTime(ISO format) and duration(hh:mm format)

Let, 让,

startDateTime => 2019-05-18T11:40:00-07:00 startDateTime => 2019-05-18T11:40:00-07:00

timeZone => -07:00 timeZone => -07:00

duration => 23:10 持续时间 => 23:10

durationHrs => 23 durationHrs => 23

durationMin => 10 durationMin => 10

endDateTime => ?? endDateTime => ?? (be in same format as of startDateTime ) (与startDateTime格式相同)

So far, I tried as below : 到目前为止,我尝试如下:

const endMs = (+new Date(startDateTime)) + (((durationHrs)*60*60+(durationMin)*60)*1000);
const endDateTime = `${new Date(endMs).toISOString().split('.')[0]}${timeZone}`;
console.log(endDateTime);

Please, take care of time zone . 请照顾时区 Thnaks in advance !! 先提前!!

There is no way to automatically convert the UTC date into some random timezone. 无法自动将UTC日期转换为某个随机时区。 It will work if the user lives in that specific timezone. 如果用户居住在该特定时区,它将起作用。 Anyway, you can use simple math to fix this problem. 无论如何,你可以使用简单的数学来解决这个问题。

Calculate the timeline different in the millisecond 计算以毫秒为单位的时间轴

const timeZone= `-07:00` 

const timezoneDiff = timeZone[0]==='-' ?  (timezone.split(':')[0]*60*60*1000 - timeZone.split(':')[1]*60*1000) : (timeZone.split(':')[0]*60*60*1000 + timeZone.split(':')[1]*60*1000)

Add the difference to the standard UNIX timestamp 将差异添加到标准UNIX时间戳

const endDateTime = `${new Date( endMs1+ (timeZoneDiff)).toISOString().split('.')[0]}${timeZone}`;

Or else use some NPM module like moment or moment-timezone 或者使用一些NPM模块,如时刻或时刻

I tried again and finally got a solution using javascript and momentjs by playing with milliseconds. 我再次尝试,最后通过玩毫秒来使用javascript和momentjs得到了解决方案。

const st = '2019-05-18T11:40:00-07:00';

const tz = '-07:00'

const dur = '23:10';

const hr = 23;

const mn = 10;


const convert = (num) => num < 10 ? "0" + num : num;
const hrs = hr * 60 * 60 * 1000;
const mins = mn * 60 * 1000;
const getms = new Date(`${st.split('-')[0]}:00`).getTime();
const totalMs = getms + hrs + mins;

const year = new Date(totalMs).getFullYear();
const month = convert(new Date(totalMs).getMonth()+1);
const date = convert(new Date(totalMs).getDate());

const getHrs = convert(new Date(totalMs).getHours());
const getMin = convert(new Date(totalMs).getMinutes());

const getendTime = `${year}-${month}-${date} ${getHrs}:${getMin}:00`;


const momentend = moment.tz(getendTime, moment.tz.guess()).clone().tz(this.$data.selectedZone.split('$')[1]).format();

console.log(momentend);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM