0

I want to convert a date.

JSON response:

{"success":1,
    "event_details":[
    {"id":"1","place_id":"0","title":"sdgf","description":"<p>gsdgs<\/p>\r\n","event_date":"1970-01-01 00:00:00","status":"1"}
    ]
}

date from json response :
2020-02-17 03:15:00

I want to convert it like :
17-2-2020

In dd/mm/YYYY format

Unnati Patadia
  • 432
  • 1
  • 10
  • 34
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) or https://stackoverflow.com/questions/1531093/how-do-i-get-the-current-date-in-javascript – Giannis Mar 06 '20 at 11:52
  • `console.log(new Date('2020-02-17 03:15:00').toLocaleDateString('hi-IN');)` – Najam Us Saqib Mar 06 '20 at 13:16

3 Answers3

0

use Moment.js to change date format-

eg:

convertDate(event_date: any) {
    return moment(event_date).format('DD-MM-YYYY');
  }
0

Install moment in your project by below command

npm install moment --save

In your page where you want to use this library import library

import Moment from 'moment'

on your code convert it

moment(event_date).format('DD-MM-YYYY');

If your purpose of doing this is only to show in UI

then you can use IONIC internal thing from here

https://ionicframework.com/docs/api/datetime

0

If you're using angular then use the DatePipe https://angular.io/api/common/DatePipe

Do something like this in your template file,

'2020-02-17 03:15:00' | date: 'dd-mm-YYYY' // -> 17-02-2020

or

class MyComponent {
   pipe = new DatePipe('en-US');
   const dateTransformed = pipe.transform('2020-02-17', 'dd-mm-YYYY');
}
haron68
  • 689
  • 1
  • 4
  • 18