0

I'm converting some date in milliseconds to date format as seen below:

var dateFrom = new Date(1312883657720); //dateFrom = 2011-08-09T12:57:01

Now I need the value of dateFrom(2011-08-09T12:57:01) in this format: 2011/08/09. I can't seem to find a date method to remove the time string, is there a workaround needed?

Brian Mogambi
  • 162
  • 1
  • 2
  • 13
  • 2
    Take a look to `momentjs`: https://momentjs.com/ – ADreNaLiNe-DJ Aug 07 '17 at 10:01
  • Probably a duplicate of [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Aug 07 '17 at 12:34

4 Answers4

1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sdf.format(new Date()));

or another method is using calender,

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
dateWithoutTime = cal.getTime();
Sai krishna
  • 168
  • 9
0

You need to form a string using the object

var dateFrom = new Date(1312883657720); //dateFrom = 2011-08-09T12:57:01
var humanDate = dateFrom.getFullYear()
                + "/" + ("0" + (dateFrom.getMonth()+1)).slice(-2)
                + "/" + ("0" + dateFrom.getDate()).slice(-2);

// ("0" + dateFrom.method()).slice(-2) is used to add the leading zero

console.log(humanDate); // 2011/08/09
Sean
  • 1,444
  • 1
  • 11
  • 21
0

Use this one

<div id="res">res</div>

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

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

    return [year, month, day].join('/');
}
document.getElementById('res').innerHTML =  formatDate('2011-08-09T12:57:01') ;

Output: 2011/08/09

Letoncse
  • 702
  • 4
  • 15
  • 36
0

Try any of the below two approaches

var newDate = moment(new Date(1312883657720)).format(YYYY/MM/DD);

or

var newDate = new Date(1312883657720);
newDate = newDate.getFullYear()+'/'+newDate.getMonth()+'/'+newDate getDate()
Praveen
  • 1,449
  • 16
  • 25
ramratan
  • 11
  • 2