0

My output consists below format February 9, 2017

How to Convert the above format to (dd,mm,yyyy) format.

Thanks in advance.

naveen n
  • 13
  • 4
  • There's a lot of extremely quirky date formats out there, but that's even more unusual than most. Is that a custom thing? – tadman Mar 10 '17 at 03:52
  • this question is quite vague. is your "output" a formatted `Date` object, or a `string`? are you trying to display the output differently, or trying to store the value differently? – Claies Mar 10 '17 at 03:54
  • Possible duplicate of [Get Date/Timestamp in a particular format](http://stackoverflow.com/questions/40372411/get-date-timestamp-in-a-particular-format) – Shubham Khatri Mar 10 '17 at 03:56

3 Answers3

0

You can try this method :

function formattedDate(d = new Date) {
  let month = String(d.getMonth() + 1);
  let day = String(d.getDate());
  const year = String(d.getFullYear());

  if (month.length < 2) month = '0' + month;
  if (day.length < 2) day = '0' + day;

  return `${day},${month}/${year}`;
}
Rai Vu
  • 1,557
  • 1
  • 17
  • 27
0

You can try with below solution:

function dateFormat(stringDate){
  var date = new Date(stringDate);
  var day = (date.getDate()>9) ? date.getDate() : "0" + date.getDate();
  var month = date.getMonth() + 1;
  month = (month>9) ? month : "0" + month;
  var year = date.getFullYear();
  // format your date as you expect
  var dateFormat = "("+year+","+month+","+day+")";
  return dateFormat;
}

var stringDate = "March 10, 2017";
var reFormat = dateFormat(stringDate);
console.log(stringDate + " => " + reFormat);
Ngoan Tran
  • 1,227
  • 1
  • 11
  • 17
0

Try as below if you prefer "dd/MM/YYYY" format

function formatDate()
{
   var str = "February 9, 2017";
   var varDate = new Date(str);
   var d = varDate.getDate();
   var m = varDate.getMonth() + 1;
   var y = varDate.getFullYear();
   var dateString = (d <= 9 ? '0' + d : d) + '/' + (m <= 9 ? '0' + m : m) + '/' + y;   
   alert(dateString);
   return false;
}

Try this if you prefer "MM/dd/YYYY" format :

function formatDate()
{
  var str = "February 9, 2017";
  var varDate = new Date(str);
  alert(varDate.toLocaleDateString());   // "MM/dd/YYYY" format
  return false;
}
SJPadikkoth
  • 87
  • 1
  • 13