Does anyone know how to parse date string in required format dd.mm.yyyy
?
Asked
Active
Viewed 2.3e+01k times
95

abatishchev
- 98,240
- 88
- 296
- 433

Azat
- 2,275
- 1
- 28
- 32
-
Do you want to create a new date object from a string in that format? – Kieran Hall Oct 16 '09 at 08:27
9 Answers
139
See:
Code:
var strDate = "03.09.1979";
var dateParts = strDate.split(".");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);

Dan Atkinson
- 11,391
- 14
- 81
- 114

Jonathan Fingland
- 56,385
- 11
- 85
- 79
-
-
2@user674887, you could compare the values after parsing. e.g. dateParts[1]-1 == date.getMonth() – Jonathan Fingland Jun 17 '11 at 21:13
-
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. – RolandoCC May 21 '14 at 14:56
-
can't believe you have to use a split to get the expected date format in javascript – Claudiu Creanga Mar 01 '16 at 20:27
-
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."`? – NanoWizard May 31 '16 at 16:09
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));

Chris Nolet
- 8,714
- 7
- 67
- 92

Mecalito
- 795
- 11
- 14
-
22+1 I don't see as being totally out of scope, JavaScript and JQuery are a common combination. – djna Sep 25 '12 at 08:02
-
An answer shouldn't rely on a library that isn't tagged or mentioned in the OP. Using [*jQuery*](http://www.doxdesk.com/img/updates/20091116-so-large.gif) as a date library is massive overkill when a 3 line function can do the same job. – RobG Aug 07 '19 at 01:01
9
We use this code to check if the string is a valid date
var dt = new Date(txtDate.value)
if (isNaN(dt))

Adriaan Stander
- 162,879
- 31
- 289
- 284
-
the given format does not match the format required by https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/parse (and thus that Date constructor will not suffice) – Jonathan Fingland Oct 16 '09 at 08:20
-
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. – RobG Mar 17 '16 at 23:01
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");

hiveer
- 660
- 7
- 17
-
2For 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. – Raptor Dec 10 '15 at 09:29
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 );

user3444722
- 31
- 1
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

Matthew Millman
- 512
- 5
- 13
2
Use Date object:
var time = Date.parse('02.02.1999');
document.writeln(time);
Give: 917902800000

HotJard
- 4,598
- 2
- 36
- 36
-
16
-
+1 For Duke comments on `new Date(Date.parse('02.02.1999'))` for Javascript – Jack Apr 26 '13 at 10:58
-
Returns NaN in at least two current browsers. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Aug 07 '19 at 01:02
-
@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. – RobG Aug 07 '19 at 01:04
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;
}

Martin Staufcik
- 8,295
- 4
- 44
- 63
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)

Bachas
- 233
- 6
- 16
-
-
-
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. – Sagar Sep 21 '17 at 06:10
-
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. – Bachas Sep 21 '17 at 06:29