The time format of JS __js

Source: Internet
Author: User
Tags dateformat

In web development, it is often a matter of time to convert to a specified string form, and if it is backstage, there is no problem (there are ready-made system methods, such as c#:datetime.tostring ("Yyyy-mm-dd")), But in the front to write a conversion method (I do not seem to have seen the ready-made JS package, do not know whether their own knowledge is too narrow), the recent collation of something, by the way to paste it out (imitation of the background of the conversion mode, but there is a situation is not compatible: DateTime.ToString (" Yyyy-mmm-dd "), the month will be converted into Chinese, I do not use the project to convert to Chinese date, so temporarily did not write; second, currently support the JSON type of time and JS built-in time type), you do not spray

function datetostring (DateFormat, currentdate) {if (null = = currentdate) {throw new Error ("The value of date
    is required ");
    //json type of time var Date_pattern =/^\/date\ (\d{13}\) \/$/; if (Date_pattern.test (currentdate)) {currentdate = new date (parseint (Currentdate.replace ("", ""). Replace (")
    /", ""))); Or else if (!
    currentdate instanceof Date) {throw new Error ("The data type is invalid");
    var year = Currentdate.getfullyear ();
    var month = Currentdate.getmonth () + 1;
    var day = Currentdate.getdate ();
    var hour = currentdate.gethours ();
    var minute = Currentdate.getminutes ();
    var second = Currentdate.getseconds ();

    var millisecond = Currentdate.getmilliseconds ();
    var Match_result;  var json = [{"P": "y+", "V": Year, "C": "Y"}, {"P": "m+", "V": Month, "C": "M"}, {"P": "D+", "V": Day, "C": "D"}, {"P": "h+", "V": Hour > Hour-12:hour, "C": "H"}, {"P": "h+", "V": Hour, "C": "H"}, {"P": "m+", "V": Minute, "C": "M"}, {"P": "S+", "V": Seco

    nd, "C": "S"}, {"P": "f+", "V": Millisecond, "C": "F"}];
    var m = eval (JSON);

        for (var i = 0; i < m.length i++) {var _reg = new RegExp (M[I].P, "G");
            while ((Match_result = _reg.exec (dateformat))!= null) {match_result = match_result[0];

            var value = M[I].V;
                        Switch (M[I].C) {case ' Y ': if (match_result.length <= 2) {
                    DateFormat = Dateformat.replace (Match_result, Value.tostring (). substr (2, 2));
                    else {DateFormat = Dateformat.replace (Match_result, value);
                } break;
                Case ' m ': Case ' d ': Case ' h ': Case ' h ': Case ' m ':
    Case ' s ':                if (match_result.length <= 1) {DateFormat = Dateformat.replace (Match_result, V
                    Alue); else {DateFormat = Dateformat.replace (Match_result, value < 10?)
                    "0" + value:value);
                } break; Case ' F ': switch (match_result.length) {case 1:d
                            Ateformat = Dateformat.replace (Match_result, Value.tostring (). substr (0, 1));
                        Break
                            Case 2:dateformat = Dateformat.replace (Match_result, Value.tostring (). substr (0, 2));
                        Break
                            Default:dateformat = Dateformat.replace (Match_result, value);
                    Break
            } break; }} return daTeformat; }


Invocation Method: Datetostring ("yyyyy year mm month DD Day", Val);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.