【问题标题】:Extract date from text从文本中提取日期
【发布时间】:2016-08-20 17:06:23
【问题描述】:

我正在使用此代码从文本中提取没有时间的日期。有没有什么方法可以在没有时间的情况下提取日期,反之亦然?

$(document).on("change", "#select_date", function() {
  const at = $('#select_date :selected').text()
  //2016-03-26 15:22:02 
  const attend_date = at.substring(0,10)
});

【问题讨论】:

  • 你可以使用正则表达式。
  • split() 更易于使用。

标签: javascript jquery date time


【解决方案1】:

您可以split()按空格选择日期并选择第一部分:

$(document).on("change", "#select_date", function() {
  const at = $('#select_date :selected').text()
  const attend_date = at.split(' ')[0]
})

【讨论】:

    【解决方案2】:
    $(document).on("change", "#select_date", function() {
      const at = $('#select_date :selected').text()
    
      //extract date
      const attend_date = at.split(' ')[0]
      //extract time
      const attend_time = at.split(' ')[1]
    })
    

    【讨论】:

    • 您应该为您的解决方案提供解释。仅代码的答案不受欢迎。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多