【问题标题】:How to combine the two time and day value in moment js如何在moment js中结合两个时间和日期值
【发布时间】:2021-05-26 05:28:14
【问题描述】:

我正在使用时刻 js,我正在努力在一个 dateTime 中组合 2 个输入 ,

我有 2 个输入值 -

从 1 日开始,我得到一天,例如星期一,星期二,星期三..星期六等..

从 2 日开始,我得到日期时间 2021 年 5 月 26 日星期三 09:20:02 GMT+0530(印度标准时间)

 dayDate  Wednesday
 timeValue  Wed May 26 2021 09:20:02 GMT+0530 (India Standard Time)

我想结合这两个值并有一个日期时间,然后需要以毫秒为单位进行转换。

例如今天是 26,用户从一个输入中选择星期六,从另一个输入中选择时间, 我必须从给定的第二个输入中获取下一个星期六的日期和时间。

我怎样才能做到这一点?

【问题讨论】:

    标签: javascript momentjs


    【解决方案1】:

    在使用.day() 设置星期几之后,首先使用从时间选择器获得的日期创建一个时刻。

    要以毫秒为单位转换日期,您可以使用.format('x')(x 是 Unix 毫秒时间戳)

    function calculateDate () {
      const dayDate = "Saturday" // replace with your get element
      const timeValue = document.getElementById("timeValue").value;
    
      console.log('Complete date: ', moment(timeValue, 'HH:mm').day(dayDate).toDate());
      console.log('In Milliseconds', moment(timeValue, 'HH:mm').day(dayDate).format('x'));
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
    
    <input type="time" onchange="calculateDate()" id="timeValue">

    如果您从时间选择器中获得完整的日期对象,则无需在时刻构造函数中指定格式,代码将变为moment(timeValue).day(dayDate).format('x')

    【讨论】:

    • 感谢回答,Complete date: Invalid Date comes 无效,“moment”:“^2.24.0”,在 json 包中,看来 .day(dayDate)
    • 在什么情况下?我在 jsfiddle 中再次尝试了 2.24.0 时刻,这是完整日期日志"Complete date: ", [object Date] { ... } 的结果。你能写一个你的代码的sn-p吗?
    • timeValue = new Date() 可以正常工作,timeValue Wed May 26 2021 09:20:34 GMT+0530 (India Standard Time) 不能正常工作
    • 我从第二个输入得到的时间有什么问题,因为timeValue = new Date() 有效,但是`2021 年 5 月 26 日星期三 09:20:34 GMT+0530(印度标准时间)` 不起作用,任何格式问题,
    • 您是否尝试将字符串转换为日期const timeValue = new Date('Wed May 26 2021 09:20:34 GMT+0530 (India Standard Time)');。这对我有用jsfiddle.net/AlTheLazyMonkey/0oufamq8/8
    【解决方案2】:

    使用startOf('week')day('Saturday')

    说明

    timeValue - 2021 年 5 月 26 日星期三 11:35:27 GMT+0530(印度标准时间)

    startOf('week') - 2021 年 5 月 23 日星期日 00:00:00 GMT+0530(印度标准时间)

    day('Saturday') - 2021 年 5 月 29 日星期六 00:00:00 GMT+0530(印度标准时间)

    const dayDate = "Saturday"
    const timeValue = new Date()
    
    console.log(moment(timeValue).startOf('week').day(dayDate).toDate())
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js" integrity="sha256-ZsWP0vT+akWmvEMkNYgZrPHKU9Ke8nYBPC3dqONp1mY=" crossorigin="anonymous"&gt;&lt;/script&gt;

    【讨论】:

    • 能否请您详细说明一下,startof week 将如何度过未来的星期六,如果今天是星期六呢
    • @JavascriptCoder 我试过解释。无论如何,您可以在提供的文档链接中通过示例找到更好的解释
    • ` dayDate 周六 timeValue Wed May 26 2021 09:20:22 GMT+0530 (India Standard Time) final scheduleDateTime Invalid Date` 它是无效日期,updatedDateTime = moment(timeValue).startOf('week').day(dayDate).toDate(); console.log("final _saveInternalSetting scheduleDateTime ", updatedDateTime); // this to be passed in put call..
    • startOfWeek =moment(timeValue).startOf('week'); updatedDateTime = moment(startOfWeek).day(dayDate).toDate(); console.log("final _saveInternalSetting scheduleDateTime ", updatedDateTime); // this to be passed in put call..。这不起作用,它给出了无效的日期.. day(dayDate).toDate()
    • 我从第二个输入中得到的时间有什么问题,因为timeValue = new Date() 有效,但`Wed May 26 2021 09:20:34 GMT+0530 (India Standard Time)` 不起作用,任何格式问题,
    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 2013-01-13
    • 2014-05-01
    • 1970-01-01
    • 2014-07-10
    • 2023-03-16
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多