-3

I want to convert string into data object in javascript.

Code Snippet:

function checkDuplicate()
{
    s = $("#fromDate0").val().split("-");
    var s1 = Date.parse(s[0], s[1] - 1, s[2]);
    alert(s1);
}

this code print garbage value for all string.

How to convert the string?

Denim Datta
  • 3,740
  • 3
  • 27
  • 53

2 Answers2

1

You probably need new Date(s[0], s[1] - 1, s[2]) instead of Date.parse().

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
1

Probably you need to concatenate your arguments for passing to parse function as it's expect only 1 argument:

var s1 = Date.parse('' + s[0] + ' ' + (s[1] - 1) + ', ' + s[2]);

Update: if your string before parsing looks like 31-10-2013 you will need short month array:

var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var s1 = Date.parse('' + monthNames[ s[ 1 ] - 1 ] + ' ' + (s[ 0 ] - 1) + ', ' + s[ 2 ]);

concatenating will build string Oct 30, 2013 and return timestamp in local time. For me it for example 1383084000000.

antyrat
  • 27,479
  • 9
  • 75
  • 76