【问题标题】:How can get only date without time from a string contain date and time?如何从包含日期和时间的字符串中仅获取没有时间的日期?
【发布时间】:2022-01-04 08:16:42
【问题描述】:

我有一个类似01/03/22 22:09 的日期,我想以类似'2022-03-01' 的日期格式将它存储在DB;我已经尝试过 Regex,但仍然可以在 一小时:分钟 内获得时间
如何删除?

const date_time = '22/01/03 22:09';
const only_date = date_time.replace(/(\d{2})\/(\d{2})\/(\d{2})/,"'20$3-$2-$1'");
//console.log(only_date.substring(0,only_date.lastIndexOf("'")+1))
console.log(only_date)

PS:不使用子字符串或切片...仅使用正则表达式有没有办法做到这一点?

【问题讨论】:

标签: javascript regex string


【解决方案1】:

你错过了.*

const date_time = '22/01/03 22:09';
const only_date = date_time.replace(/(\d{2})\/(\d{2})\/(\d{2}).*/,"'20$3-$2-$1'");
console.log(only_date)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2015-05-11
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多