0

I need some help. I need to create a new date object in mm/dd/yyyy format from a string of format 24-Mar-2015.

user3497581
  • 31
  • 2
  • 7
  • `Date` objects don't "know" about a format. You take a `Date` object and perform whatever formatting you want on it... – Jon Skeet Apr 20 '15 at 13:35
  • possible duplicate of [Format date in java](http://stackoverflow.com/questions/4772425/format-date-in-java) – Zielu Apr 20 '15 at 14:28

4 Answers4

3

You can use SimpleDateFormat.

String src = "24-Mar-2015";
Date date = new SimpleDateFormat("d-MMM-yyyy").parse(src);
String dst = new SimpleDateFormat("MM/dd/yyyy").format(date);
Bubletan
  • 3,833
  • 6
  • 25
  • 33
2

In JavaScript

function getFormattedDate(date) {
      var year = date.getFullYear();
      var month = (1 + date.getMonth()).toString();
      month = month.length > 1 ? month : '0' + month;
      var day = date.getDate().toString();
      day = day.length > 1 ? day : '0' + day;
      return year + '/' + month + '/' + day;
    }
    alert(getFormattedDate(new Date(2015,3,24))); // 2015/03/24
gariepy
  • 3,576
  • 6
  • 21
  • 34
ozil
  • 6,930
  • 9
  • 33
  • 56
1

The Date(String) constructor can be used to create date object from String, which can then be formatted by SimpleDateFormat.

  String date = "24-Mar-2015";
 System.out.println(new SimpleDateFormat("dd-MMM-yyyy").format(new Date(date)));

*To print month in full use SimpleDateFormat("dd-MMMM-yyyy") instead.

Arjun Lajpal
  • 208
  • 1
  • 11
0

In JavaScript:

var myDate = new Date('2015/01/23');
myDate.toString(); // "Fri Jan 23 2015 00:00:00 GMT+0100 (CET)"
dsuckau
  • 592
  • 3
  • 15