Date format convert javascript

2020-04-08 10:58发布

I am trying to convert "July 24 2013" to "DD-MM-YYYY" with javascript but I keep getting and error.

I am using new Date('July 24 2013').format("DD-MM-YYYY");

What am I missing?

5条回答
ら.Afraid
2楼-- · 2020-04-08 11:35

Date object doesn't have .format() method. If you work with date and time actively I'd recommend you to use MomentJS library then. It has a lot of useful functionality to work with time and date.

For example: moment('July 24 2013', 'MMMM D YYYY').format('MMMM D YYYY, h:mm:ss a');

查看更多
够拽才男人
3楼-- · 2020-04-08 11:41

To simplify a little bit...the problem is because the format used by browser to store dates is not always the same (and the one you're passing to Date constructor is not allowed). It's allowed by the standard, what is required is the browser can parse the format produced by itself (see §15.9.4.2).

Usually to work directly with date format is not a good idea, not only because browser specific implementation but because of globalization (this is especially true for a web application with a virtual world wide audience).

The only format you're sure every browser will read is YYYY-MM-DDTHH:mm:ss.sssZ (see §15.9.1.15) so in your case you should change your custom parsing/formatting to that.

Take a look to this and this posts on SO for other details (or this little tutorial about dates).

What I suggest, if you work with dates across browsers and locales, is to use a good library to abstract all this details. I found this one is pretty strong and easy to use (it replaces standard Date functions so you may not even need to change one single line of code).

查看更多
时光不老,我们不散
4楼-- · 2020-04-08 11:43

I would highly advise using momentjs!

moment('July 24 2013').format("DD-MM-YYYY");
// => "24-07-2013"

Simple as that!

查看更多
三岁会撩人
5楼-- · 2020-04-08 11:48

There is no Data.prototype.format function in JavaScript.

You're best off looking at open source options for localizing dates. I've had success with

There's also:

查看更多
你好瞎i
6楼-- · 2020-04-08 11:52

Bellow is simple, portable, pure JS implementation based on Java date format:

function date_format( d, p ) {
    var pad = function (n, l) {
        for (n = String(n), l -= n.length; --l >= 0; n = '0'+n);
        return n;
    };
    var tz = function (n, s) {
        return ((n<0)?'+':'-')+pad(Math.abs(n/60),2)+s+pad(Math.abs(n%60),2);
    };
    return p.replace(/([DdFHhKkMmSsyZ])\1*|'[^']*'|"[^"]*"/g, function (m) {
        l = m.length;
        switch (m.charAt(0)) {
                case 'D': return pad(d.getDayOfYear(), l);
                case 'd': return pad(d.getDate(), l);
                case 'F': return pad(d.getDayOfWeek(i18n), l);
                case 'H': return pad(d.getHours(), l);
                case 'h': return pad(d.getHours() % 12 || 12, l);
                case 'K': return pad(d.getHours() % 12, l);
                case 'k': return pad(d.getHours() || 24, l);
                case 'M': return pad(d.getMonth() + 1, l );
                case 'm': return pad(d.getMinutes(), l);
                case 'S': return pad(d.getMilliseconds(), l);
                case 's': return pad(d.getSeconds(), l);
                case 'y': return (l == 2) ? String(d.getFullYear()).substr(2) : pad(d.getFullYear(), l);
                case 'Z': return tz(d.getTimezoneOffset(), ' ');
                case "'":
                case '"': return m.substr(1, l - 2);
                default: throw new Error('Illegal pattern');
        }
    });
};
console.log( date_format( new Date(), 'yyyy.mm.dd kk:MM:ss Z' ) );
console.log( date_format( new Date(), 'MM/dd/yyyy HH:mm:ss' ) );

Above code is based on http://llamalab.com/js/date/Date.js (LGPL)

查看更多
登录 后发表回答