【问题标题】:react js date format with moment用时刻反应js日期格式
【发布时间】:2020-05-07 07:33:34
【问题描述】:

在此处输入代码 在此处输入代码

var start_date = ["2020-05-07 18:30:00"];
var header_date_array =  ["2020-05-07 12:59:12 PM", "2020-05-07 14:29:12 PM", "2020-05-07 18:30:00 PM", "2020-05-07 20:29:12 
PM"];


//match or compare date with same format 
//what i should i do?

//this is my logic, but it is not working
 const isSameDate = (start_date, header_date) => {
   const startDate = moment(start_date);
   const headerDate = moment(header_date_array);

   return startDate.isSame(headerDate, 'day');
 }
console.log(isSameDate);

我在 React js 中遇到了问题。我想比较/匹配两个日期start_endheader_date 并显示在渲染组件页面上。

我的代码示例:

          {this.state.appointmentdata.map(data => 
              { const dateTime = moment(data.start_date.toString()); //start_date
                const headerDate = moment(this.state.headerdatearray);    //header_date
                return headerDate.isSame(dateTime, 'day');
            })
          }

console.log 数据(日期格式)

start_date: 2020-05-07 02:15:00     //format: "YYYY-MM-DD HH:mm:ss"

header_date array: ["11:57:16 AM", "11:27:16 AM"] 

但是日期格式不同。如何获得相同格式的相同日期?

有人帮帮我吗?

【问题讨论】:

  • 没有得到你..两者都是时刻对象,所以你不必担心它们被记录的格式..或者是你的差异? ??????你可以使用moment方法来比较日期..
  • 你能.format() 把它们变成标准格式吗?它们都是 Moment 对象,所以格式是无关紧要的。
  • 是的,我试试.format()@drew-reese
  • moment?这是比较两个日期的正确逻辑吗@panther
  • 您有更好的方法吗?我如何比较两个日期@panther

标签: javascript reactjs date momentjs date-parsing


【解决方案1】:

标题日期是一个数组,因此您可能希望对其进行迭代,为每个对象构造一个 luxon DateTime 对象,并检查它是否与指定的开始日期具有相同的 second。检查seconds 单位包括所有 个更大的单位,即相同的分钟、小时、日、周等...只要一些 标题日期相同,则返回true .

编辑:我无法使用momenjs 使格式按预期工作,并且由于您听起来对其他建议持开放态度,IMO luxon 是处理 DateTime 对象的更好实用程序(它由同一组织,顺便说一句)

const isSameDate = (start_date, header_date_array) => {
  const startDate = DateTime.fromFormat(start_date, 'yyyy-MM-dd HH:mm:ss');
  return header_date_array.some(header_date => {
    const headerDate = DateTime.fromFormat(header_date, 'yyyy-MM-dd hh:mm:ss a');
    return startDate.hasSame(headerDate, "second");
  });
};

给定标题日期数组和约会数据:

const header_date_array = [
  "2020-05-07 12:59:12 PM",
  "2020-05-07 14:29:12 PM",
  "2020-05-07 18:30:00 PM",
  "2020-05-07 20:29:12 PM"
];

const appointmentData = [
  {
    start_date: "2020-05-07 18:30:00"
  },
  {
    start_date: "2020-05-07 09:30:00"
  },
  {
    start_date: "2020-05-08 18:30:00"
  },
  {
    start_date: "2020-05-08 09:30:00"
  }
];

这样的地图

appointmentData.map(({ start_date }) =>
  isSameDate(start_date, header_date_array)
)

[true, false, false, false]

【讨论】:

  • 我实现了你的代码,它们只匹配day 我怎么能在瞬间匹配day with time @drew
  • startDate.isSame(headerDate, "daytime"); 喜欢这样吗? @drew
  • @MHasan 您可以简单地指定一个较小的单位,即second,它还会检查所有较大的单位(分钟、小时、天等)。我在将它们解析为有效的时刻对象时遇到问题,因此转换为 luxon。更新了答案。
  • 如何导入luxon ?安装的任何软件包@Drew
  • 它无法正常工作luxon @Drew
【解决方案2】:

可以有多种选择:

在终端中运行这个命令:

npm install moment --save

在 React js 组件中的使用:

import Moment from 'moment';

render(){
    Moment.locale('en');
    var dt = '2020-05-07T02:15:00';
    return(<View> {Moment(dt).format('d MMM')} </View>)
}

你可以在这里查看moment.js官方文档https://momentjs.com/docs/

或者你可以使用这个包

npm install date-fns --save

代码中的用法:

import { format } from "date-fns";

var date = new Date("2020-05-07 02:15:00");

var formattedDate = format(date, "MMMM Do, YYYY H:mma");

console.log(formattedDate);

【讨论】:

    【解决方案3】:

    根据您当前的代码结构,我尝试如下:

    var start_date = ["2020-05-07 18:30:00"];
    var header_date_array =  ["2020-05-07 12:59:12 PM", "2020-05-07 14:29:12 PM", "2020-05-07 18:30:00 PM", "2020-05-07 20:29:12 
    PM"];
    

    如果 start_date 是数组,

    this.start_date.map(start_date => {
      this.isSameDate(start_date, this.header_date_array);
      console.log(this.isSameDate(start_date, this.header_date_array));
    })
    
    const isSameDate = (start_date, header_date) => {
      const startDate = [moment.utc(start_date).format('L'), moment.utc(start_date).format('LTS')].join(' '); // You can replace LTS with LT to eliminate seconds.
      let flag;
      header_date.map(d => {
        d = d.substring(0, d.indexOf("PM")); // if I don't remove "PM", then moment says invalid Date. I have not dug in-depth to find reason.
        const headerDate = [moment.utc(d).format('L'), moment.utc(d).format('LTS')].join(' '); //Again here You can replace LTS with LT to eliminate seconds.
        startDate == headerDate ? flag = headerDate : flag = 'no appointment logged';
      })
      return flag; // you can use 'start_date' in order to return date instead.
    }
    

    【讨论】:

    • 很难看懂你的代码@Shoma
    • 可以分享任何示例代码codesandbox (@Shoma)[stackoverflow.com/users/7901403/shoma]
    • 您是在比较“11:57:16 AM”和“11:27:16 AM”吗?
    • 不与start_date: 2020-05-07 02:15:00 比较每个header date: ["11:57:16 AM", "11:27:16 AM"] 如果start_date 存在或不存在@Shoma,他们会检查一个数组
    • 我已经编辑了我的答案,看看你能不能看懂,让timeDiff = m1.diff(m2);如果日期不同,可以退回您。通过这种方式,您可以为自己修改。似乎还缺少一件事,即数组中的日期,它将被视为今天的日期时间,你说什么?
    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2019-11-02
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多