代码之家  ›  专栏  ›  技术社区  ›  XMehdi01

如何从包含日期和时间的字符串中仅获取没有时间的日期?

  •  -2
  • XMehdi01  · 技术社区  · 4 年前

    01/03/22 22:09 我想把它储存在 DB 只是以这样的方式约会 '2022-03-01' ; 我试过了 正则表达式 一小时:分钟

    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:不使用子字符串或切片。。。就用regex有没有办法做到这一点?

    2 回复  |  直到 4 年前
        1
  •  2
  •   mplungjan    4 年前

    你错过了比赛 .*

    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)
        2
  •  -3
  •   Adriaan Avinash Kumar Jha    4 年前

    date_time.replace(/(\d{2})\/(\d{2})\/(\d{2}).<b>*</b>/,"'20$3-$2-$1'");