0

How to convert the string which is having date time to date time format. My code is:

      In Chrome its working fine:

      var str = "05-Sep-2013 01:05:15 PM " 
      var res = Date.parse(str) 
      console.log(res) //o/p:1378366515000
      var result = new Date(res)
      console.log(result) //o/p:Thu Sep 05 2013 13:05:15 GMT+0530 (India Standard Time)

     In Firefox and IE:
     console.log(res) //o/p: NaN
     console.log(result) //o/p: Date {Invalid Date}

Could you please help me out. thanks in advance.

Venky
  • 15
  • 2
  • 1
    convert dashes to slashes – SoluableNonagon Aug 25 '16 at 14:10
  • https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – Xotic750 Aug 25 '16 at 14:13
  • Thanks@ SoluableNonagon , var str = "05-Sep-2013 01:05:15 PM " ; str1 = str.replace(/-/g, '/'); var res = Date.parse(str1); console.log(res); var result = new Date(res) console.log(result); o/p : 1378366515000 Date {Thu Sep 05 2013 13:05:15 GMT+0530 (India Standard Time)} But i want "Thu Sep 05 2013 13:05:15 GMT+0530 (India Standard Time)" only how we will take. – Venky Aug 25 '16 at 14:15
  • @Venky can you put that in your question as an update? tough to read code in comments. – SoluableNonagon Aug 25 '16 at 14:19
  • You have the date, you can create whatever format you like once you get the milliseconds. – SoluableNonagon Aug 25 '16 at 14:21
  • the date is returning like this :Date {Thu Sep 05 2013 13:05:15 GMT+0530 (India Standard Time)} I want Thu Sep 05 2013 13:05:15 GMT+0530 (India Standard Time) in this format. please help me – Venky Aug 25 '16 at 14:24
  • You can format it however you like using the [`Date` methods](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date) – Xotic750 Aug 25 '16 at 14:26
  • Ext has parsing code to normalize browser differences. Check out `Ext.Date`. – Evan Trimboli Aug 26 '16 at 10:44

2 Answers2

0

Parse the string yourself as suggested on

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

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

I gave you a link to an answer on SO that explains how to do this.

Converting String into date format in JS

This example should work even on very old or very broken browsers.

var lookupMonthName = {
  jan: 0,
  feb: 1,
  mar: 2,
  apr: 3,
  may: 4,
  jun: 5,
  jul: 6,
  aug: 7,
  sep: 8,
  oct: 9,
  nov: 10,
  dec: 11
};

function customParse(dateTimeStr) {
  var dateTime = dateTimeStr.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '').split(' ');
  var date = dateTime[0].split('-');
  date[1] = lookupMonthName[date[1].toLowerCase()].toString();
  date.reverse();
  var time = dateTime[1].split(':');
  if (dateTime[2].toUpperCase() === 'PM') {
    time[0] = (parseInt(time[0], 10) + 12).toString();
  }
  var args = date.concat(time);
  console.log(args);
  return new Date(Date.UTC.apply(null, args));
}

var str = '05-Sep-2013 01:05:15 PM ';
var date = customParse(str);
document.getElementById('out').appendChild(document.createTextNode(date));
console.log(date);
<pre id="out"></pre>

To format a string from a Date object, see SO answers

Where can I find documentation on formatting a date in JavaScript?

A little effort on your part and you would have been able to find this information yourself.

Community
  • 1
  • 1
Xotic750
  • 22,914
  • 8
  • 57
  • 79
-1

IE and FF would like '/' instead of '-' in dates

http://plnkr.co/edit/9ZoHwjvgMA2twEoTJTn9?p=preview

var str = "05-Sep-2013 01:05:15 PM ";
console.log( Date.parse( str ) );  // NaN

console.log( Date.parse( str.replace(/-/g, '/') ) ); // 1378404315000

So parsing gets you the milliseconds, now you can just put it in a date:

var d = new Date( Date.parse( str.replace(/-/g, '/') ) );
console.log( d ); // 2013-09-05T18:05:15.000Z

And there it is, same as your input date but in diff format.

SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98
  • Its not working in IE, could you please help me out. – Venky Aug 25 '16 at 15:10
  • As per https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/parse Parse the string yourself, create a new Date object with the data and the use then Date methods to format your required string. There are lots of example here on SO. An example of Parsing your string. http://stackoverflow.com/questions/27720916/converting-string-into-date-format-in-js/27721275#27721275 – Xotic750 Aug 25 '16 at 15:39
  • Thanks @Xotic750, console.log( Date.parse( str.replace(/-/g, '/') ) ); // o/p NaN in IE. please help me. – Venky Aug 26 '16 at 06:41
  • Have you read the docs that I linked and the parsing example that I linked on SO? – Xotic750 Aug 26 '16 at 07:16