Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  75] [ 6]  / answers: 1 / hits: 122646  / 6 Years ago, thu, may 10, 2018, 12:00:00

I am working in an angular 4 application, Here I need to get the current Date and Time Using angular DatePipe.



I want to get the date and time in the following format



dd-mm-yyyy hh:MM:ss AM/PM



I got the expected by using the Angular DatePipe as follows



<p>{{today | date:'dd-MM-yyyy hh:mm:ss a':'+0530'}}</p> 


output :



10-05-2018 03:28:57 PM


Here I What I want to do is get the same output from my app.component.ts without touching the HTML's



So I tried the below code but it generates a 13 digit timestamp



today = Date.now();
fixedTimezone = this.today;


SO how can I get the date and time as the mentioned format purely from app.component.ts file without using HTML's.


More From » angular

 Answers
19
let dateFormat = require('dateformat');
let now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");



Thursday, May 10th, 2018, 7:11:21 AM 



And this format is exactly like your question


dateFormat(now, "dd, mm, yyyy, h:MM:ss TT"); 

returns 10, 05, 2018 7:26:57 PM


you need npm package npm i dateformat
here is a link for the npm package https://www.npmjs.com/package/dateformat


Here is another question that inspires me How to format a JavaScript date




h:MM:ss TT results 7:26:57 PM


HH:MM:ss results 13:26:57


Here is it https://jsfiddle.net/5z1tLspw/


I hope that helps.


[#54470] Sunday, May 6, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susanajamiep

Total Points: 466
Total Questions: 113
Total Answers: 108

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
susanajamiep questions
Sun, Jun 12, 22, 00:00, 2 Years ago
Mon, Mar 7, 22, 00:00, 2 Years ago
Wed, Jun 10, 20, 00:00, 4 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
;