获取已经过去的时间,考虑到用户的TimeZone with Moment
Get the time that has been passed, taking into account the user's TimeZone with Moment
我正在用 React Native 开发一个 Web 应用程序来显示事件列表。这些事件来自配置为 GMT -4 且位于 America/Santo_Domingo 的后端,作为时区。
我从后端收到的事件日期是 2020-11-05T16:02:13.1018115
。
我的活动画面是这样的:
Title: Hi, a party has started!
Description: 1 hour ago // <= could be year, month, day, hour, minute or second
问题: 当我new Date()
进行计算时,一些设备报告不同的日期。这些日期有几个小时的差异。例如,使用时区 America/La_Paz
或 Etc/GMT+12
.
的设备
我的结论是我需要考虑设备的时区来进行计算。我正在关注 this guide and I have installed Moment Timezone。
我将从后端收到的日期传递给此函数:
export const timeSince = (backEndTimeStamp) => {
// get device timezone eg. -> "America/La_Paz"
const deviceTimeZone = 'America/La_Paz';
// Make moment of right now, using the device timezone
const today = moment().tz(deviceTimeZone);
// Get the UTC offset in hours
const currentTimeZoneOffsetInHours = today.utcOffset() / 60;
const convertedToLocalTime = formatTimeByOffset(backEndTimeStamp, currentTimeZoneOffsetInHours);
const years = today.diff(convertedToLocalTime, 'years');
const months = today.diff(convertedToLocalTime, 'months');
const days = today.diff(convertedToLocalTime, 'days');
const hours = today.diff(convertedToLocalTime, 'hours');
const minutes = today.diff(convertedToLocalTime, 'minutes');
const seconds = today.diff(convertedToLocalTime, 'seconds');
let timeDiff = '';
if (years > 0) {
timeDiff = years + (years === 1 ? ' year' : ' years');
} else if (months > 0) {
timeDiff = months + (months === 1 ? ' month' : ' months');
} else if (days > 0) {
timeDiff = days + (days === 1 ? ' day' : ' days');
} else if (hours > 0) {
timeDiff = hours + (hours === 1 ? ' hour' : ' hours');
} else if (minutes > 0) {
timeDiff = minutes + (minutes === 1 ? ' minute' : ' minutes');
} else if (seconds > 0) {
timeDiff = seconds + (seconds === 1 ? ' second' : ' seconds');
} else {
timeDiff = 'unknown';
}
return timeDiff;
}
此函数处理转换:
export const formatTimeByOffset = (dateString, offset) => {
// Params:
// How the backend sends me a timestamp
// dateString: on the form yyyy-mm-dd hh:mm:ss
// offset: the amount of hours to add.
// If we pass anything falsy return empty string
if (!dateString) return '';
if (dateString.length === 0) return '';
// Step 1: Parse the backend date string
// Get Parameters needed to create a new date object
const year = dateString.slice(0, 4);
const month = dateString.slice(5, 7);
const day = dateString.slice(8, 10);
const hour = dateString.slice(11, 13);
const minute = dateString.slice(14, 16);
const second = dateString.slice(17, 19);
// Step: 2 Make a JS date object with the data
const dateObject = new Date(`${year}-${month}-${day}T${hour}:${minute}:${second}`);
// Step 3: Get the current hours from the object
const currentHours = dateObject.getHours();
// Step 4: Add the offset to the date object
dateObject.setHours(currentHours + offset);
// Step 5: stringify the date object, replace the T with a space and slice off the seconds.
const newDateString = dateObject
.toISOString()
.replace('T', ' ')
.slice(0, 19);
// Step 6: Return the new formatted date string with the added offset
return `${newDateString}`;
}
一些用户看到 1-4 小时之间的差异。
示例:
Today: Thu Nov 05 2020 19:0803 GMT-0400
Date from backend: 2020-11-05T16:27:40.687
Date converted to local time using the function above: 2020-11-05 12:27:40
Result: 6 hours // <= Has an extra hour
我尝试了很多东西,我有点困惑。
我的目标是获取已经过去的时间,同时考虑到用户的时区。知道后端使用 GMT -4 向我发送日期。
感谢任何帮助。
试试这个
var localTime = moment.utc("2020-11-05T16:02:13.1018115").add(4, 'hour').local().fromNow();
我正在用 React Native 开发一个 Web 应用程序来显示事件列表。这些事件来自配置为 GMT -4 且位于 America/Santo_Domingo 的后端,作为时区。
我从后端收到的事件日期是 2020-11-05T16:02:13.1018115
。
我的活动画面是这样的:
Title: Hi, a party has started!
Description: 1 hour ago // <= could be year, month, day, hour, minute or second
问题: 当我new Date()
进行计算时,一些设备报告不同的日期。这些日期有几个小时的差异。例如,使用时区 America/La_Paz
或 Etc/GMT+12
.
我的结论是我需要考虑设备的时区来进行计算。我正在关注 this guide and I have installed Moment Timezone。
我将从后端收到的日期传递给此函数:
export const timeSince = (backEndTimeStamp) => {
// get device timezone eg. -> "America/La_Paz"
const deviceTimeZone = 'America/La_Paz';
// Make moment of right now, using the device timezone
const today = moment().tz(deviceTimeZone);
// Get the UTC offset in hours
const currentTimeZoneOffsetInHours = today.utcOffset() / 60;
const convertedToLocalTime = formatTimeByOffset(backEndTimeStamp, currentTimeZoneOffsetInHours);
const years = today.diff(convertedToLocalTime, 'years');
const months = today.diff(convertedToLocalTime, 'months');
const days = today.diff(convertedToLocalTime, 'days');
const hours = today.diff(convertedToLocalTime, 'hours');
const minutes = today.diff(convertedToLocalTime, 'minutes');
const seconds = today.diff(convertedToLocalTime, 'seconds');
let timeDiff = '';
if (years > 0) {
timeDiff = years + (years === 1 ? ' year' : ' years');
} else if (months > 0) {
timeDiff = months + (months === 1 ? ' month' : ' months');
} else if (days > 0) {
timeDiff = days + (days === 1 ? ' day' : ' days');
} else if (hours > 0) {
timeDiff = hours + (hours === 1 ? ' hour' : ' hours');
} else if (minutes > 0) {
timeDiff = minutes + (minutes === 1 ? ' minute' : ' minutes');
} else if (seconds > 0) {
timeDiff = seconds + (seconds === 1 ? ' second' : ' seconds');
} else {
timeDiff = 'unknown';
}
return timeDiff;
}
此函数处理转换:
export const formatTimeByOffset = (dateString, offset) => {
// Params:
// How the backend sends me a timestamp
// dateString: on the form yyyy-mm-dd hh:mm:ss
// offset: the amount of hours to add.
// If we pass anything falsy return empty string
if (!dateString) return '';
if (dateString.length === 0) return '';
// Step 1: Parse the backend date string
// Get Parameters needed to create a new date object
const year = dateString.slice(0, 4);
const month = dateString.slice(5, 7);
const day = dateString.slice(8, 10);
const hour = dateString.slice(11, 13);
const minute = dateString.slice(14, 16);
const second = dateString.slice(17, 19);
// Step: 2 Make a JS date object with the data
const dateObject = new Date(`${year}-${month}-${day}T${hour}:${minute}:${second}`);
// Step 3: Get the current hours from the object
const currentHours = dateObject.getHours();
// Step 4: Add the offset to the date object
dateObject.setHours(currentHours + offset);
// Step 5: stringify the date object, replace the T with a space and slice off the seconds.
const newDateString = dateObject
.toISOString()
.replace('T', ' ')
.slice(0, 19);
// Step 6: Return the new formatted date string with the added offset
return `${newDateString}`;
}
一些用户看到 1-4 小时之间的差异。
示例:
Today: Thu Nov 05 2020 19:0803 GMT-0400
Date from backend: 2020-11-05T16:27:40.687
Date converted to local time using the function above: 2020-11-05 12:27:40
Result: 6 hours // <= Has an extra hour
我尝试了很多东西,我有点困惑。
我的目标是获取已经过去的时间,同时考虑到用户的时区。知道后端使用 GMT -4 向我发送日期。
感谢任何帮助。
试试这个
var localTime = moment.utc("2020-11-05T16:02:13.1018115").add(4, 'hour').local().fromNow();