Date format convert javascript

后端 未结 5 1092
再見小時候
再見小時候 2021-01-19 01:01

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-YY

相关标签:
5条回答
  • 2021-01-19 01:18

    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).

    0 讨论(0)
  • 2021-01-19 01:21

    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

    • Globalize: https://github.com/jquery/globalize#dates

    There's also:

    • Moment: http://momentjs.com/timezone/
    • DateJS: https://code.google.com/p/datejs/
    0 讨论(0)
  • 2021-01-19 01:28

    I would highly advise using momentjs!

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

    Simple as that!

    0 讨论(0)
  • 2021-01-19 01:34

    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');

    0 讨论(0)
  • 2021-01-19 01:35

    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)

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