如何将日期,时间和日期与react Native代码中的标题分开?

发布于 2025-02-10 00:25:06 字数 728 浏览 1 评论 0 原文

我的API日期和时间格式是这样的: ”在此处输入图像描述

我目前正在使用此方法在我的代码中渲染日期和时间。

let d = new Date(item.starts_on);
let date = `${d.getMonth() + 1}/${d.getDate()}/${d.getFullYear()}`;
let time = `${d.getHours()}:${d.getMinutes()}`;

但我想以这种格式呈现 “在此处输入图像描述” 请帮助我

My API Date and Time Format is like this: enter image description here

I'm currently using this method to render the date and time in my code.
enter image description here

let d = new Date(item.starts_on);
let date = `${d.getMonth() + 1}/${d.getDate()}/${d.getFullYear()}`;
let time = `${d.getHours()}:${d.getMinutes()}`;

But i want to render it in this format enter image description here Pls do help me out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2025-02-17 00:25:06

有两种方法可以这样做,使用 dayjs 通过使用它,您可以处理日期并转换为许多不同的形状信息检查它们的文件)。 如果您想将日期时间与时间戳分开

/*
  other your imports and codes
*/
let timestamp = moment(item.starts_on);
let date = timestamp.format('L');
let time = timestamp.format('LT');
/*
  other your codes
*/

例如, 日期,您可以:

/*
  other your imports and codes
*/
let d = new Date(item.starts_on);
let date = `${d.getMonth() + 1}/${d.getDate()}/${d.getFullYear()}`;
let time = `${d.getHours()}:${d.getMinutes()}`;
/*
  other your codes
*/

更新答案
对于将日期转换为“ 2022年3月19日星期六”,您想要的,以下内容以下内容:

//fill in Months array
const Months = ["Jan", "Feb", ..., "Dec"];
//fill in Days of week array
const Days = ["Sun", "Mon", ..., "Sat"];
/*
  other your codes
*/
let d = new Date(item.starts_on);
let monthIndex = d.getMonth();
let month = Months[monthIndex];
let dayIndex = d.getDay();
let day = Days[dayIndex];

let date = `${day} ${d.getDate()} ${month} ${d.getFullYear()}`;
/*
  other your codes
*/

There are two approach for doing it, first way (that is easier) using packages like moment or dayjs by using it you are be able to handle date and convert to many different shapes (for more information check the documents of them). For example if you want to separate date and time from a timestamp you can do it by moment like below:

/*
  other your imports and codes
*/
let timestamp = moment(item.starts_on);
let date = timestamp.format('L');
let time = timestamp.format('LT');
/*
  other your codes
*/

Second way is using at instance of Date, for doing it you can:

/*
  other your imports and codes
*/
let d = new Date(item.starts_on);
let date = `${d.getMonth() + 1}/${d.getDate()}/${d.getFullYear()}`;
let time = `${d.getHours()}:${d.getMinutes()}`;
/*
  other your codes
*/

Update Answer
For converting date to like "Sat 19 Mar 2022" you want, followings below:

//fill in Months array
const Months = ["Jan", "Feb", ..., "Dec"];
//fill in Days of week array
const Days = ["Sun", "Mon", ..., "Sat"];
/*
  other your codes
*/
let d = new Date(item.starts_on);
let monthIndex = d.getMonth();
let month = Months[monthIndex];
let dayIndex = d.getDay();
let day = Days[dayIndex];

let date = `${day} ${d.getDate()} ${month} ${d.getFullYear()}`;
/*
  other your codes
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文