tags:

views:

97

answers:

4

hi,

i have a date in this format : dd.mm.yyyy

when i instantiate a javascript date with it, it gives me a NaN

in c# i can specify a dateformat, to say: here you have my string, it's in this format, please make a Datetime of it.

is this possible in javascript too, and if not, is there an easy way?

i would prefer not to use a substring for day, substring for month etc. because my method must also be capable of german, italian, english etc dates.

A: 

There is no built in way to manipulate dates the way you would like.

The jQuery-UI datepicker has the functionality you want, I'm sure many other libraries have something similar.

$.datepicker.parseDate('dd.mm.yy', '31.12.2007');
Ryley
yeay, this is something i want, but without the whole datepicker lib if possible. So i would like one of those 'may other libraries' :-)
Michel
@Michel check out "datejs", which is used by the jQuery UI datepicker
Pointy
@Pointy - perfect, I was just finding the same thing
Ryley
+2  A: 

You will need to create a function to extract the date parts and use them with the Date constructor.

Note that this constructor treats months as zero based numbers (0=Jan, 1=Feb, ..., 11=Dec).

For example:

function parseDate(input) {
  var parts = input.match(/(\d+)/g);
  // note parts[1]-1
  return new Date(parts[2], parts[1]-1, parts[0]);
}

parseDate('31.05.2010');
// Mon May 31 2010 00:00:00

Edit: For handling a variable format you could do something like this:

function parseDate(input, format) {
  format = format || 'yyyy-mm-dd'; // default format
  var parts = input.match(/(\d+)/g), 
      i = 0, fmt = {};
  // extract date-part indexes from the format
  format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });

  return new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);
}

parseDate('05.31.2010', 'mm.dd.yyyy');
parseDate('31.05.2010', 'dd.mm.yyyy');
parseDate('2010-05-31');

The above function accepts a format parameter, that should include the yyyy mm and dd placeholders, the separators are not really important, since only digits are captured by the RegExp.

You might also give a look to DateJS, a small library that makes date parsing painless...

CMS
i would like to process the mm/dd/yyyy and dd/mm/yyyy too
Michel
@Michel give a look to my edit.
CMS
Excellent! works like a charm! hate it though that i have no clue how it works :)
Michel
+1  A: 

It's easy enough to split the string into an array and pass the parts directly to the Date object:

var str = "01.01.2010";
var dmy = str.split(".");

var d = new Date(dmy[2], dmy[1] - 1, dmy[0]);
Andy E
Date()'s month parameter takes Month in Zero based index. January = 0, february = 1, march = 2... etc
ItzWarty
@ItzWarty: yes, I just remembered that too, thanks :-)
Andy E
A: 
t="01.01.1970"
parts = t.split(".");
for(var i = 0; i < parts.length; i++) parts[i] = parseInt(parts[i], 10);
new Date(parts[2], parts[1]-1, parts[0]);

Date defined as (Year, Month, Date)
Date()'s month parameter takes Month in Zero based index. January = 0, february = 1, march = 2... etc

Parsing the string to an int isn't necessary, but I dislike passing strings into functions and just hoping that JavaScript will "get it"... Sort of like how some people prefer ===

ItzWarty
`parseInt` shouldn't be required (JavaScript's typing is dynamic and strings should automatically be cast to a number) and the loop is a bit OTT for such a simple operation.
Andy E
What @Andy says is that the `Date` constructor will [explicitly](http://bclary.com/2004/11/07/#a-15.9.3.1) convert the arguments internally [`ToNumber`](http://bclary.com/2004/11/07/#a-9.3). Also, using `parseInt` without the radix argument, is specially harmful here, since you can have a date like `"09.09.2010"`, and `parseInt('09') == 0`, since the preceding zero will force the number conversion as octal base, and this doesn't happen with the internal `ToNumber` operation.
CMS
Good point, CMS.
ItzWarty