95

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

1
  • Do you want to create a new date object from a string in that format? Commented Oct 16, 2009 at 8:27

9 Answers 9

139

See:

Code:

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

var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
Sign up to request clarification or add additional context in comments.

5 Comments

mmm, but how to check strDate has a invalid value like "30.30.2000"?
@user674887, you could compare the values after parsing. e.g. dateParts[1]-1 == date.getMonth()
If you have date both TIME like this: "2014-05-20T16:43:56.71-06:00" var partesFecha = solicitud.CreatedDate.split("T")[0].split("-"); var createdDate = new Date(partesFecha[0], (partesFecha[1] - 1), partesFecha[2]); First extract date before T and later split year, month and day.
can't believe you have to use a split to get the expected date format in javascript
Anyone else find it odd that the Mozilla reference article on the Date object itself says "Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies."?
51

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/

2 Comments

+1 I don't see as being totally out of scope, JavaScript and JQuery are a common combination.
An answer shouldn't rely on a library that isn't tagged or mentioned in the OP. Using jQuery as a date library is massive overkill when a 3 line function can do the same job.
9

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

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

2 Comments

the given format does not match the format required by developer.mozilla.org/en/Core_JavaScript_1.5_Reference/… (and thus that Date constructor will not suffice)
That doesn't test for valid dates at all, it just tests if the Date constructor can make a valid date from the input. The two aren't the same thing.
7

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

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

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

ex:

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

1 Comment

For most cases, MomentJS is an overkill, as using pure JavaScript just requires hundreds of bytes, while MomentJS is 12.4kb gzipped. Convenient for developers but slows down the website. Doesn't worth.
3

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

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

Comments

2

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

1 Comment

OP does not mention he uses ASP.NET
2

Use Date object:

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

Give: 917902800000

4 Comments

new Date(Date.parse('02.02.1999'))
+1 For Duke comments on new Date(Date.parse('02.02.1999')) for Javascript
Returns NaN in at least two current browsers. See Why does Date.parse give incorrect results?
@Duke— new Date(Date.parse('02.02.1999')) will produce identical results to new Date('02.02.1999'), including Invalid Date in some browsers. Using the built–in parser is arguably the worst way to parse a timestamp.
2

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;
}

Comments

0

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)

4 Comments

You're supposed to use javascript not jquery as there is no jquery tag
happy ... meaning is to just understand the senario
Well. I don't think your example is any different than the one given by Martin Staufcik. In fact his answer has additional logic to handle invalid dates.
Martin Staufcik handle those conditions which does not occurs generally. can you give me any calendar which can give invalidates.why we should write so long code when we can handle in short way. It would me much better if Martin Staufcik also give to handle date with time to handle.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.