1

I have string 02-APR-15 11:08 AM which i have to convert DateTime format in javascript.I am used

var date = Date.parse('02-APR-15 11:08 AM');
alert(date);

which shows NaN .How to convert this string to DateTime Format.

1
  • You need to format the date at first. Better use moment.js for date manipulation Commented Apr 2, 2015 at 5:52

5 Answers 5

1
var inputDate='02-APR-15 11:08 AM';
inputDate =inputDate.replace(/-/g, ' ');
var result = new Date(inputDate);
alert(result);
Sign up to request clarification or add additional context in comments.

1 Comment

this too will not work in FF. This alerts invalid date.
1

Correct it will not work in FF. Have a look at this code.

var dateString = '02-APR-2015 11:08 AM';
var d = dateString.split(" ");
dArray = d[0].split("-");
var day = dArray[0];
var month = dArray[1];
switch(dArray[1]) {
    case "JAN" :
        month = "01";
        break;
    case "FEB" :
        month = "02";
        break;
    case "MAR" :
        month = "03";
        break;
    case "APR" :
        month = "04";
        break;
    case "MAY" :
        month = "05";
        break;
    case "JUN" :
        month = "06";
        break;
    case "JUL" :
        month = "07";
        break;
    case "AUG" :
        month = "08";
        break;
    case "SEP" :
        month = "09";
        break;
    case "OCT" :
        month = "10";
        break;
    case "NOV" :
        month = "11";
        break;
    case "DEC" :
        month = "12";
        break;
    default :
        month = "01";
        break;        
}
var year = dArray[2];
var tm = d[1].split(":");
    if(d[2] == "AM") {
        var tm_h = tm[0];
   } else {
       var tm_h = tm[1]+12;
   }
   var tm_m = tm[1];
       var newStr = year+"-"+month+"-"+day+"T"+tm_h+":"+tm_m+":"+"00";

var date = new Date(newStr);
alert(date);

Fiddle : https://jsfiddle.net/uuv7uv3h/

Comments

0
var date = Date.parse(response.date);
alert(date.toString());

Comments

0

Use this instead :

date = new Date('02-APR-15 11:08 AM');
alert(date);

Comments

0

Date.parse returns a timestamp which is an integer when a valid date string is supplied, otherwise NaN. So you need to convert your date string to a valid string like:

var s = '02-APR-15 11:08 AM'.replace(/(\d{1,2})-(\w+)-(\d{2})/, '$2 $1 20$3')
date = new Date(s);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.