最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get date and time in mm-dd-yyy hh-mm-ss in typescript? - Stack Overflow

programmeradmin3浏览0评论

Currently, I am getting date time in the following format 'mm/dd/yyyy, hh:mm:ss'. How to get it in the following format 'mm-dd-yyyy hh-mm-ss'.

How to achieve this without using any library, and preferably by passing some args to the function itself?

Below is the code that am currently using (in Angular 5)

console.log(new Date().toLocaleString(undefined, { hour12: false }));

Currently, I am getting date time in the following format 'mm/dd/yyyy, hh:mm:ss'. How to get it in the following format 'mm-dd-yyyy hh-mm-ss'.

How to achieve this without using any library, and preferably by passing some args to the function itself?

Below is the code that am currently using (in Angular 5)

console.log(new Date().toLocaleString(undefined, { hour12: false }));

Share Improve this question edited Aug 2, 2018 at 8:02 Bahman Parsa Manesh 2,3683 gold badges18 silver badges36 bronze badges asked Aug 2, 2018 at 7:13 Rohan AgarwalRohan Agarwal 2,6093 gold badges19 silver badges37 bronze badges 2
  • Hi, the solution to the question you provided is the one that I am using in the first place. However, I want to format THAT output to a particular format, without the use of momentjs or writing any particular formatting function. Thanks :) – Rohan Agarwal Commented Aug 2, 2018 at 7:19
  • @GONZALOPANIAGUA - its not duplicate , its question for angular and angular has DatePipe for doing such formating ..no need of long javascript code – Pranay Rana Commented Aug 2, 2018 at 7:26
Add a ment  | 

1 Answer 1

Reset to default 4

make use of DatePipe , that is provided by angular framework

{{ strDate | date :'MM-dd-yyyy hh-mm-ss' }

or in code you can do like this

import { DatePipe } from '@angular/mon';

@Component({
 selector: 'test-ponent',
  templateUrl: './test-ponent.ponent.html'
})
class TestComponent {

  constructor(private datePipe: DatePipe) {}

  formatDate(date= new Date()) {
    return this.datePipe.transform(date,'MM-dd-yyyy hh-mm-ss' );
  }
}

check here : DatePipe

发布评论

评论列表(0)

  1. 暂无评论