-1

I have getting a dateformat like '23.10.2017'. I need to format this in to

'10/23/2017'

I just tried

var crDate='23.10.2017';
var newDateF=new Date(crdate).toUTCString();

but it showing InvalidDate

can anyone help to change the format. Thanks in advance

Arun
  • 1,402
  • 10
  • 32
  • 59

3 Answers3

1

I don't think using Date() is the solution. You can do

var crDate = '23.10.2017';
var newDateF = crDate.split(".");
var temp = newDateF[0];
newDateF[0] = newDateF[1];
newDateF[1] = temp;
newDateF.join("/");

This splits the string into an array, swaps the first and second elements, and then joins back on a slash.

vityavv
  • 1,482
  • 12
  • 23
1

A regex replacement will do the trick without any Date functions.

var date = '23.10.2017';
var regex = /([0-9]{2})\.([0-9]{2})\.([0-9]{4})/;
console.log(date.replace(regex,'$2/$1/$3'));
Jon Uleis
  • 17,693
  • 2
  • 33
  • 42
0

Just use moment.js if you can :

// convert a date from/to specific format
moment("23.10.2017", "DD.MM.YYYY").format('MM/DD/YYYY')

// get the current date in a specific format
moment().format('MM/DD/YYYY')

Moment is a very usefull and powerfull date/time library for Javascript.

Indent
  • 4,675
  • 1
  • 19
  • 35