-7

I need to covert the string which contains date and time looks like this

"27-JAN-15 08.30.00.000000 AM"

When I use new Date("27-JAN-15 08.30.00.000000 AM") I get error asInvalid Date.

Please do help.

Thanks

Balwant Kumar Singh
  • 1,158
  • 4
  • 24
  • 48
  • you want both date and time? or just date? – Codeek Dec 31 '14 at 12:08
  • 4
    *"When I use new Date("27-JAN-15 08.30.00.000000 AM") I get error asInvalid Date."* Right. Because there's nothing whatsoever that claims that the `Date` constructor can parse that format. So *you* have to. Once you give it a try, if you run into trouble, ask a specific question about the code you're having trouble with. – T.J. Crowder Dec 31 '14 at 12:10

3 Answers3

2
var dateVal = "27-JAN-15 08.30.00.000000 AM";
console.log(new Date(dateVal.split(".").join(":")));
Jeroen
  • 60,696
  • 40
  • 206
  • 339
Rohith K
  • 1,433
  • 10
  • 15
1
var myDate = function(dateString) {
    var dateString = dateString || "27-JAN-15 08.30.00.000000 AM" // An example
    var newDate = new Date(dateString.split(" ")[0]);
    var hours = dateString.split(" ")[2]==="AM" ? dateString.split(" ")[1].split(".")[0] : parseInt(dateString.split(" ")[1].split(".")[0], 10) + 12;
    newDate.setHours(hours);
    newDate.setMinutes(dateString.split(" ")[1].split(".")[1]);
    newDate.setSeconds(dateString.split(" ")[1].split(".")[2]);
    newDate.setMilliseconds(dateString.split(" ")[1].split(".")[3]);
    return newDate;
}
user1496463
  • 410
  • 3
  • 14
  • This will probably work, but it would be cleaner to split only once and store the result in a variable. – JJJ Dec 31 '14 at 12:17
0

Manual parsing is the way to go.

"27-JAN-15 08.30.00.000000 AM"

First split you string at the "spaces", giving you

"27-JAN-15"

"08.30.00.000000"

"AM"

Now you can take the date part and split at the -, giving you

"27"

"JAN"

"15"

Now you can convert the month by using an object as a lookup table to give you a numeric value.

So JAN will give you 0, you now have

"27"

"0"

"15"

The year part is now ambiguous, Date will take values from 0 to 99 and map to the years 1900 to 1999. So you will need to decide how you are going to deal with this based on your data.

Now the last 2 strings, you have

"08.30.00.000000"

"AM"

The "AM" or "PM" can be ignored as the time is in 24 hour format, so now split the time string at ., giving you

"08"

"30"

"00"

"000000"

The Date constructor only handles and accuracy of milliseconds, so you could take the "000000" and slice the first 3 digits, giving you

"000"

Now take all the parts that you have manually parsed and use them with the date constructor

new Date(year, month[, day[, hour[, minute[, second[, millisecond]]]]]);

so,

new Date("15", "0", "27", "08", "30", "00", "000");

You will now have a javascript local date object, without cross browser parse inconsistencies.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

If your date is UTC then you may want to use

Date.UTC()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC

Xotic750
  • 22,914
  • 8
  • 57
  • 79