Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Can  I change a date/time from 2014-08-20 15:30:00 to look like 08/20/2014 3:30 pm using javascript's Date object?

1 Answer

0 votes
by (19.7k points)

Yes, javascript Date() object is used to work with dates and times.

 Refer to the code implementation below: 

function to_format_date(date) {

  var hrs = date.getHours();

  var min = date.getMinutes();

  var am_pm = hrs >= 12 ? 'pm' : 'am';

  hrs = hrs % 12;

  hrs = hrs ? hrs : 12; 

  min = min < 10 ? '0'+min : min;

  var time = hrs + ':' + min + ' ' + am_pm;

  return (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear() + "  " + Time;

}

var d = new Date();

var e = to_format_date(d);

alert(e);

 Interested in Java? Check out this Java Certification by Intellipaat.

Related questions

0 votes
1 answer
asked Apr 4, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Jan 28, 2021 in Java by dante07 (13.1k points)
–1 vote
1 answer
0 votes
1 answer
asked Jun 8, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer

Browse Categories

...