【问题标题】:ngx bootstrap datepicker doesn't support epoch date format?ngx bootstrap datepicker 不支持纪元日期格式?
【发布时间】:2019-01-31 07:26:35
【问题描述】:

例如,2003 年 6 月 13 日 23:11:52.454 UTC 的纪元日期格式是 1055545912454。但是当我在 bsValue 中传递这个值时,我得到了未定义。

要求 我的要求是在发出服务器请求时,日期应该在 api 中以纪元格式发送。因此,我编写了一个指令,它将任何日期格式转换为毫秒(changeDateDirective),当发生这种情况时,bsdatepicker 返回 undefined <input changeDateDirective type="text" placeholder="Datepicker" class="form-control" #dp="bsDatepicker" bsDatepicker [(bsValue)]="bsValue" [minDate]="bsValue" [bsConfig]="{ showWeekNumbers:false, selectFromOtherMonth: true}" > 在 angular ui datepicker(uib-datepicker - https://angular-ui.github.io/bootstrap/) 中同样有效

【问题讨论】:

    标签: javascript angular datepicker angular-ui ngx-bootstrap


    【解决方案1】:

    bsValue 需要 Javascript's Date 的对象,因此您必须传递一个 Date 实例。 可以使用 Unix 时间戳(自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数)构造 Date 实例

    let date = new Date(1055545912454);
    // Date 2003-06-13T23:11:52.454Z
    bsValue = date;
    

    【讨论】:

    • 谢谢,但我的要求是在发出服务器请求时,日期应该在 api 中以纪元格式发送。所以,我写了一个指令,它将任何日期格式转换为毫秒(dateToMillis),当这种情况发生时 bsdatepicker 返回 undefined 同样适用于 Angular ui datepicker(uib-datepicker)
    • 为什么您不只是在将日期发送到后端(即在序列化程序)之前将其转换为 Unix 时间戳?
    • 因为我们必须在使用日期选择器的任何地方一次又一次地转换它,而不是我想要一个通用的解决方案,因此需要指令
    • 然后将您的 ngx-bootstrap-datepicker 包装在一个组件中。然后你可以在那里处理转换,并可以在你的应用程序的任何地方重用组件。
    • 我可以试试。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多