-1

Let say I have an object product

I can use product.createdDate to get 2018-04-16 15:12:46.179427 or any other date with the same format.

how do I convert to date in javascript so I and show it as 16 April 2018?

I'm using angularjs.

I tried {{product.createdDate | date : 'dd MM, yyyy'}}but it show 2018-04-16 15:12:46.179427

Daniel Mana
  • 5,951
  • 10
  • 23
  • 38
  • 2
    Possible duplicate of [Converting string to date in js](https://stackoverflow.com/questions/5619202/converting-string-to-date-in-js) – Makyen Apr 23 '18 at 05:54

4 Answers4

3

There is no straightforward way to do it. You can find your answer in a similar question here :

Get current date in DD-Mon-YYY format in JavaScript/Jquery

Shashaank V V
  • 520
  • 1
  • 3
  • 18
1

You can use date pipe to filter it out

html

<p>
  {{date | date:'dd MMMM yyyy'}}
</p>

In your controller

$scope.date = new Date("2018-04-16 15:12:46.179427");

To get directly in controller inject $filter

var date = $filter('date')(new Date("2018-04-16 15:12:46.179427"), 'dd MMMM yyyy');

console.log(date); // 16 April 2018

Please refer the demo link

Abinesh Joyel
  • 1,817
  • 2
  • 8
  • 15
1

You can try the following:

var date = new Date("2018-04-16 15:12:46.179427".replace(/-/g,"/"));
var d = moment(date).format('D MMMM  YYYY')
console.log(d);
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
Md Johirul Islam
  • 4,523
  • 2
  • 20
  • 52
0

To convert this date using javascript you have to define the months like bellow.

const months = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

const date = new Date('2018-04-16 15:12:46.179427');
const formatedDate = date.getDate() + ' ' + months[date.getMonth()] +' '+ date.getFullYear();

But if you only want to show it in the UI, use a pipe to convert it.

Ajantha Bandara
  • 1,153
  • 10
  • 25