How can I convert string to datetime with format specification in JavaScript?

前端 未结 15 1020
终归单人心
终归单人心 2020-11-22 02:35

How can I convert a string to a date time object in javascript by specifying a format string?

I am looking for something like:

var dateTime = convert         


        
15条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 03:08

    You can use the moment.js library for this. I am using only to get time-specific output but you can select what kind of format you want to select.

    Reference:

    1. moment library: https://momentjs.com/

    2. time and date specific functions: https://timestamp.online/article/how-to-convert-timestamp-to-datetime-in-javascript

    convertDate(date) {
            var momentDate = moment(date).format('hh : mm A');
            return momentDate;
    }
    

    and you can call this method like:

    this.convertDate('2020-05-01T10:31:18.837Z');
    

    I hope it helps. Enjoy coding.

提交回复
热议问题