[ACCEPTED]-Parse DateTime string in JavaScript-datetime-parsing

Accepted answer
Score: 139



var strDate = "03.09.1979";
var dateParts = strDate.split(".");

var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);


Score: 51

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

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


var myDate = '30.11.2011';
var parsedDate = $.datepicker.parseDate('dd.mm.yy', myDate);

$('#date1').text($.datepicker.formatDate('M d, yy', parsedDate));


Score: 9

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

var dt = new Date(txtDate.value)
if (isNaN(dt))
Score: 7

refs: http://momentjs.com/docs/#/parsing/string/

If you use moment.js, you can use 1 "string" + "format" mode

moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);
moment(String, String, String, Boolean);


moment("12-25-1995", "MM-DD-YYYY");
Score: 3

I'v been used following code in IE. (IE8 1 compatible)

var dString = "2013.2.4";
var myDate = new Date( dString.replace(/(\d+)\.(\d+)\.(\d+)/,"$2/$3/$1") );
alert( "my date:"+ myDate );
Score: 2

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

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


Score: 2

Use Date object:

var time = Date.parse('02.02.1999');

Give: 917902800000


Score: 2

This function handles also the invalid 29.2.2001 1 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;
Score: 0

you can format date just making this type 1 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)

More Related questions