【问题标题】:Get month from datefield从日期字段获取月份
【发布时间】:2015-02-22 06:56:39
【问题描述】:

我使用 Django-rest,我想在 AngularJS 中获取月份形式的 Datefield。

我的代码是date = '2014-2-30',我得到月份:var x = (new Date (Date.parse (date).getMonth()

但是x = 1year = 114 的值。

我读了https://docs.angularjs.org/api/ng/filter/date,但我做不到。你能举个例子吗?

【问题讨论】:

  • 你的 datefiled 是一个 javascript 对象还是只是一个日期字符串?
  • 你能把一些相关的代码粘贴到plunkr吗?
  • 我的日期字段是 django 库中的模型 DateField
  • 我的代码是:date = '2014-2-30';我得到月份:var x = (new Date(Date.parse("1-5-2012"))).getMonth().但我的价值 x = 1 和年价值 = 114 不 = 2014

标签: angularjs date django-rest-framework


【解决方案1】:

Django REST Framework 应该以 ISO 8601 格式 (yyyy-MM-dd) 将日期传递回 Angular,因此在 Angular 端解析它应该没有问题。

{{ obj.date | date:"MMMM" }}

MMMM 是整月的 Angular 日期代码 (January-December)。您可以找到其他 Angular 日期代码in the date filter documentation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2016-08-20
    • 2021-10-20
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多