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?

- 12,279
- 5
- 36
- 59

- 756
- 5
- 15
-
where you and how you using ... please some code – harkesh kumar Dec 12 '18 at 12:41
-
asking something by just typing 2 line not give you any ans – harkesh kumar Dec 12 '18 at 12:41
-
I want to send the current datetime in a request, which accepts a string – Akash Srivastav Dec 12 '18 at 12:50
-
Possible duplicate of [Convert Epoch time to human readable with specific timezone](https://stackoverflow.com/questions/44060804/convert-epoch-time-to-human-readable-with-specific-timezone) – SomethingDark Dec 12 '18 at 14:34
3 Answers
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.

- 7,161
- 1
- 31
- 52
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.

- 3,737
- 4
- 33
- 56