Parse DateTime string in JavaScript

前端 未结 9 2182
别跟我提以往
别跟我提以往 2020-11-22 12:15

Does anyone know how to parse date string in required format dd.mm.yyyy?

相关标签:
9条回答
  • 2020-11-22 12:52

    you can format date just making this type of the code.In javascript.

     // for eg.
                  var inputdate=document.getElementById("getdate").value);
                     var datecomp= inputdate.split('.');
    
                    Var Date= new Date(datecomp[2], datecomp[1]-1, datecomp[0]); 
                     //new date( Year,Month,Date)
    
    0 讨论(0)
  • 2020-11-22 12:54

    ASP.NET developers have the choice of this handy built-in (MS JS must be included in page):

    var date = Date.parseLocale('20-Mar-2012', 'dd-MMM-yyyy');
    

    http://msdn.microsoft.com/en-us/library/bb397521%28v=vs.100%29.aspx

    0 讨论(0)
  • 2020-11-22 12:55

    This function handles also the invalid 29.2.2001 date.

    function parseDate(str) {
        var dateParts = str.split(".");
        if (dateParts.length != 3)
            return null;
        var year = dateParts[2];
        var month = dateParts[1];
        var day = dateParts[0];
    
        if (isNaN(day) || isNaN(month) || isNaN(year))
            return null;
    
        var result = new Date(year, (month - 1), day);
        if (result == null)
            return null;
        if (result.getDate() != day)
            return null;
        if (result.getMonth() != (month - 1))
            return null;
        if (result.getFullYear() != year)
            return null;
    
        return result;
    }
    
    0 讨论(0)
  • 2020-11-22 13:02

    We use this code to check if the string is a valid date

    var dt = new Date(txtDate.value)
    if (isNaN(dt))
    
    0 讨论(0)
  • 2020-11-22 13:04

    If you are using jQuery UI, you can format any date with:

    <html>
        <body>
            Your date formated: <span id="date1"></span><br/>
        </body>
    </html>
    

     

    var myDate = '30.11.2011';
    var parsedDate = $.datepicker.parseDate('dd.mm.yy', myDate);
    
    $('#date1').text($.datepicker.formatDate('M d, yy', parsedDate));
    

    http://jsfiddle.net/mescalito2345/ND2Qg/14/

    0 讨论(0)
  • 2020-11-22 13:05

    See:

    • Mozilla Core JavaScript Reference: Date object
    • Mozilla Core JavaScript Reference: String.Split

    Code:

    var strDate = "03.09.1979";
    var dateParts = strDate.split(".");
    
    var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
    
    0 讨论(0)
提交回复
热议问题