-2

I need to convert the current ISO Date to the required format . How ? Mongodb iso date: 2016-08-31T08:30:17.795Z

required format type: Aug 31,2016 08:30 AM

Sid
  • 11
  • 2

3 Answers3

0

I think it is recommended to store date as ISO format into the database. If you want to display on the client side with AngularJS. Just initial a new date object as below.

var date = new Date('2016-08-31T08:30:17.795Z')

It will return as

Wed Aug 31 2016 16:30:17 GMT+0800 (Malay Peninsula Standard Time)

And after that, you can use some built-in function to extract date, time or hour like

date.getDate()  
date.getSeconds()

And next time please search on StackOverflow first because there are a lot of answer for this kind of question :D

Refer here for more information.

trungk18
  • 19,744
  • 8
  • 48
  • 83
0

Date conversion:

parse the date

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",   Locale.ENGLISH);

For formatting in your date

DateFormat formatterWithTime = new SimpleDateFormat("MMM dd,YYYY HH:mm a", Locale.ENGLISH);
System.out.println(formatterWithTime.format(formatter.parse("2016-08-31T08:30:17.795Z")));

Output : Aug 31,2016 08:30 AM

Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28
paresh
  • 351
  • 1
  • 3
  • 12
0

Using the mongo java driver, you do not have to supply the date in a given (string-) format. Instead, you simply use java.util.Date objects.

mtj
  • 3,381
  • 19
  • 30