在类型脚本中将日期字符串ddmmyy转换为dd / mm / yyyy

问题描述 投票:0回答:1

在日期选择器onchange方法中,我已将日期作为字符串传递,如ddmmyyyy(20102020)到dd / mm / yyyy。我不知道如何将日期格式ddmmyyyy转换为dd / mm / yyyy。

<input class="form-control"
                               placeholder="DD/MM/YYYY"
                               ngbDatepicker
                               formControlName="dateofBrith"
                               [minDate]="{year: 1975, month: 1, day: 1}"
                               [maxDate]="{year: 2020, month: 12, day: 31}"
                               name="dp"
                               ngbDatepicker
                               #d="ngbDatepicker"
                               (ngModelChange)="changeDob($event)"
                               maxlength="10">
                        <div class="input-group-append">
                            <button class="btn calendar btn-custom btn-outline-secondary"
                                    (click)="d.toggle()"
                                    type="button"><img src="/assets/images/dateicon.png"></button>
                        </div>
changeDob(value){}

在这种方法中,我从日期选择器单击中获取的值为{year:2020,month:10,day:10}。与此同时,用户输入日期而不是在输入字段中单击,我会以字符串形式接收值。我不知道如何转换格式,也不知道要继续。请理解我的问题。

angular typescript frontend bootstrap-datepicker
1个回答
0
投票

尝试使用moment.js库进行日期和时间转换。他们具有内置功能可以准确处理此问题。

https://momentjs.com/

Ex moment('your time string')。format('dd / mm / yyyy')将给出所需的结果。

或者您可以使用有角日期管道

import { Component, OnInit, Inject,LOCALE_ID } from '@angular/core';
import { formatDate } from '@angular/common';

export class AngularpipeComponent implements OnInit {

datePipeString : string;

constructor(@Inject(LOCALE_ID) private locale: string) { 
  this.datePipeString = formatDate(Date.now(),'dd/mm/yyyy',this.locale);
  console.log(this.datePipeString);
}
© www.soinside.com 2019 - 2024. All rights reserved.