-4

I need a current date in DD:MM:YYYY HH:MM:SS Format, but getting a numeric one. I'm using Date.now() for the same. How should I set the format to get the required format?

David Walschots
  • 12,279
  • 5
  • 36
  • 59
Akash Srivastav
  • 756
  • 5
  • 15

3 Answers3

3

if you use Angular and typescript, you can use "DatePipe", with transform method, like that:

datePipe.transform(date, 'dd/MMM/yyyy hh:mm:ss');

if you use javascript, look at @benshabatnoam answer

veben
  • 19,637
  • 14
  • 60
  • 80
2

Your question is not related to angular, typescript or angular 6.

Date.now() is a pure javascript function

There are lots of ways to format a numeric date as you can see in this question's answers.

One posibble way without any other 3rd party is:

let date = new Date(Date.now());
date = `${date.getDate()}:${date.getMonth() + 1}:${date.getFullYear()}:${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;

EDIT: From your question it is not clear what is your need by formatting the date, but if you're needing this format only in your view, and you do use angular, then @veben answer is the best approach for you, use angular's DatePipe.

benshabatnoam
  • 7,161
  • 1
  • 31
  • 52
1

If I'm getting your question right, you need a date in your controller class.

  • If you don't want to use any third party library, simply use datePipe. Here is a running stackblitz example.

  • Also, you could efficiently use moment js here as follows -

var customDate = moment().format('DD:MM:YYYY hh:mm:ss');
console.log(customDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Here, you may always change the format as you need.

Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56