reactjs - 将时间戳转换为日期和时间(带时区)的任何解决方案?在 React.js 中
问题描述
我试图构建一个日历组件,但在我的后端 Jason Api 上,日期以时间戳给出。那么我将如何将时间戳转换为日期和时间(带时区)?
考虑下面的代码。
const appointments = [
{
id: 0,
title: 'Watercolor Landscape',
startDate: new Intl.DateTimeFormat('en-US').format(1585640511000), // here to convert timestamp
endDate: new Date(2018, 6, 23, 11, 30),
ownerId: 1,
}
];
constructor(props) {
super(props);
this.state = {
data: appointments,
};
this.commitChanges = this.commitChanges.bind(this);
}
render() {
const { data } = this.state;
console.log( data) // I console the data
return (
<Paper>
<Scheduler
data={data}
>
)
}
这是我控制台的结果:
我的问题是如何将 startDate 的类型转换为与 endDate 相同的类型?
解决方案
你可以使用moment.js。它提供了一种将字符串解析为 Date 对象的简单方法。
var date = moment("3/31/2020", "M/DD/YYYY");
推荐阅读
- java - 不支持的媒体类型 415 - 上传照片
- angular - 以角度 4 在一个组件与另一个组件之间共享数据
- sql-server - mssql和mvc,需要邮件调度
- java - 我想创建一个具有特殊属性的二维静态数组,其中每个元素都是一对元素的哈希映射?
- json - 如何在 Swift 中解码 JSON,它是一个包含双嵌套项的数组?
- php - Xdebug 提前终止,没有错误,但站点在禁用时运行良好
- python-2.7 - 如何通过“邮件”在 Linux 服务器上用 Python 发送邮件?
- java - 以预建字典为数据结构的压缩算法
- ios - 在合适的滚动视图中显示各种表格视图
- php - 如何在不复制/粘贴的情况下在多页网站上的 head 标签内生成相同的 HTML?