Formatting the date time with Javascript

后端 未结 12 2232
暗喜
暗喜 2020-11-27 07:36

I have a date/time string like 2012-01-13 04:37:20 but I want to convert it to dd-mm-yyyy hh:mm, how can i do this?

I am using the followin

相关标签:
12条回答
  • 2020-11-27 08:03

    I think it is best to use the Intl.DateTimeFormat class.

    The usage is fairly straightforward. You can not enter a pattern as you want to, but it will give you the results you want.

    Here is an example on how to use it:

    public formatDate(date : Date) : string{
        var options = {  year: 'numeric', month: 'short', day: 'numeric' };
        return new Intl.DateTimeFormat('de-DE', options).format(date);
    }
    

    If you really want to enter a DateTimeFormat string, it would be easy enough to write a function which parses the string using Regex, but I don't think it is needed.

    For further Information go here:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

    0 讨论(0)
  • 2020-11-27 08:05

    For working with DateTimes in javascript it's better to use the 'Intl.DateTimeFormat' as follow:

    var date = new Date('2012-01-13 14:37:20');
    var options = { year: 'numeric', month: '2-digit', day: '2-digit',
    hour:'2-digit', minute: '2-digit',hour12: false};
    console.log(new Intl.DateTimeFormat('en-US', options).format(date).replace(/\//g,'-').replace(',',''));
    

    Result: "01-13-2012 14:37"

    The date and time formats can be customized with options argument.

    Check Online

    0 讨论(0)
  • 2020-11-27 08:05

    Love one liners - local date SPACE time DOT milliseconds / IIFE:

    // simpler, but milliseconds not padded
    console.log(
    (function(d){return d.toLocaleDateString() + ' ' + d.toLocaleTimeString() + '.' + d.getMilliseconds()})(new Date())
    )
    
    // including millis padding
    console.log(
    (function(d){return d.toLocaleDateString() + ' ' + d.toLocaleTimeString() + '.' + (d.getMilliseconds()+1000+'').substr(1)})(new Date())
    )

    0 讨论(0)
  • 2020-11-27 08:07

    a small function , as follow:

    var formatTime = function(time, format){
    time = typeof time == 'number' ? new Date(time) : time;
    format = format || 'yyyy-mm-dd hh:MM:ss';
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    var add0 = function(t){return t < 10 ? '0' + t : t}
    var replaceMent = {
        'yyyy': year,
        'mm': add0(month),
        'm': month,
        'dd': add0(date),
        'd': date,
        'hh': add0(hours),
        'h': hours,
        'MM': add0(minutes),
        'M': minutes,
        'ss': add0(seconds),
        's': seconds
    }
    for( var k in replaceMent ){
        format = format.replace(k, replaceMent[k]);
    }
    return format;
    

    }

    0 讨论(0)
  • 2020-11-27 08:08

    Use either simple string manipulation (as suggested by @SKS) or use a library. The latter is more flexible and lets you change the input or output format easily. For example, using the Globalize.js library, you would write:

    var dd = Globalize.parseDate(now, "yyyy-MM-dd HH:mm:ss");
    dd = Globalize.format(dd, "dd-MM-yyyy HH:mm");
    

    Note however that formats such as "dd-mm-yyyy hh:mm" are confusing – it is neither a standard ISO format nor any localized (language-dependent) format. The Globalize.js library lets you use predefined language-dependent formats in addition to explicitly specified formats.

    Note that the built-in date and time parsing and formatting routines in JavaScript are implementation-dependent. Using them means non-portable code. For example, there is no guarantee that new Date() will accept the format you have as input, and toLocaleDateString() writes the date in some locale-dependent format, which can be just about anything.

    0 讨论(0)
  • 2020-11-27 08:10

    Date requires Date object so you should give var d = new Date() something like this then for formatting see http://code.google.com/p/datejs/ link it will be helpful.

    0 讨论(0)
提交回复
热议问题