2

I have a string in JavaScript 2015-07-22 00:00:00.0. I need this to be converted to mm/dd/yy format

I tried parsing with var d = new Date(from_date);, which works in Chrome,but not in IE or Firefox

Then I tried with regex pattern replace, but it still works only in Chrome but not in IE or Firefox

var st = '2015-07-22 00:00:00.0'
var pattern = '\d{4})\-(\d{2})\-(\d{2})/'
var dt = new Date(st.replace(pattern, '$2-$3-$1'));
var output = dt.getMonth() + 1 + '/' + dt.getDate() + '/' + dt.getFullYear();
alert(output) 

JSFiddle

Kindly throw some light.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
sathishkumar
  • 337
  • 1
  • 3
  • 10

3 Answers3

3

If the date is always in that format, you can substring it out and use the standard date format to get it working.

var r = "2015-07-22 00:00:00.0"
var date = new Date(r.substring(0, 4),   //year
                    r.substring(5, 7),   //month
                    r.substring(8, 10),   //day
                    r.substring(11, 13), //hour
                    r.substring(14, 16), //min
                    r.substring(17, 19), //sec
                    r.substring(20));    //mili
UnstableEagle
  • 562
  • 2
  • 16
  • 3
    @JoshStevens because if you can't control the format of the string and it's not one recognized, parsing it for the values and explicitly setting them in the proper arguments is the best way to do it. – CrayonViolent Jul 16 '15 at 15:20
  • @JoshStevens Wow, no need to be rude... The reason is that the only standard accepted datestrings are one of these formats: [`"October 13, 1975 11:13:00"`, `"October 13, 1975 11:13"`, or `"October 13, 1975"`](http://stackoverflow.com/questions/3505693/difference-between-datedatestring-and-new-datedatestring) of which none of these are. Sure this is simple, but your answer could technically not work due to it passing an unstandard format to the constructor. By breaking this up, we avoid any compatability issues. EDIT And he deleted his answer after being rude... Some people – UnstableEagle Jul 16 '15 at 15:22
  • Hi guys thanks for looking in to this,and early turn around when i tried with this code i got the date as Fri Jan 02 201 00:00:00 GMT+0100 (W. Europe Standard Time) But if u see my date string its totally different date https://jsfiddle.net/m1tht293/ – sathishkumar Jul 16 '15 at 17:31
  • @sathishkumar Could you put an edit in your main question? Include the code, then comment here when finished and I'll take a look at it – UnstableEagle Jul 16 '15 at 17:33
  • @unstable eagle i havent added any code just tried your code and print the output and result date is different thanks again – sathishkumar Jul 16 '15 at 17:38
  • @sathishkumar had an issue with substring numbers. Fixed them. This now works! – UnstableEagle Jul 16 '15 at 17:56
2

Whenever you do anything with date and or time in Javascript, I would advice you to use momentJS

In your case you should do something like this to parse the string:

var x = moment(st, "YYYY-MM-DD");

Then for output:

console.log( x.format("MM/DD/YYYY") );
Sander_P
  • 1,787
  • 1
  • 13
  • 37
-1

This should work fine for you.

var date = new Date('2015-07-22 00:00:00.0'.replace(/ /g, 'T')),
  monthDigits = [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12' ];

console.log(monthDigits[date.getMonth()] + '/' + date.getDate() + '/' + date.getFullYear());
mrdeleon
  • 545
  • 4
  • 15