Does anyone know how to parse date string in required format dd.mm.yyyy?
9 Answers
See:
Code:
var strDate = "03.09.1979";
var dateParts = strDate.split(".");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
    5 Comments
Dr. No
 mmm, but how to check strDate has a invalid value like "30.30.2000"?
  Jonathan Fingland
 @user674887, you could compare the values after parsing. e.g. dateParts[1]-1 == date.getMonth()
  RolandoCC
 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.
  Claudiu Creanga
 can't believe you have to use a split to get the expected date format in javascript
  NanoWizard
 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."?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));
    We use this code to check if the string is a valid date
var dt = new Date(txtDate.value)
if (isNaN(dt))
    2 Comments
Jonathan Fingland
 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)
  RobG
 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.
  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
Raptor
 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.
  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
Raptor
 OP does not mention he uses ASP.NET
  Use Date object:
var time = Date.parse('02.02.1999');
document.writeln(time);
Give: 917902800000
4 Comments
Duke
 new Date(Date.parse('02.02.1999'))Jack
 +1 For Duke comments on 
  new Date(Date.parse('02.02.1999')) for JavascriptRobG
 Returns NaN in at least two current browsers. See Why does Date.parse give incorrect results?
  RobG
 @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.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
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
Sagar
 You're supposed to use javascript not jquery as there is no jquery tag
  Bachas
 happy ... meaning is to just understand the senario
  Sagar
 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.
  Bachas
 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.