【问题标题】:How to get day of month from Date() [duplicate]如何从 Date() 获取月份中的某天 [重复]
【发布时间】:2020-07-27 16:55:24
【问题描述】:

如何在javascript中从Date()获取月份的日期(数字)?

我试试这个:

const date = new Date(`2020-07-21T08:08:20.794Z`);

const dayNumber = date.getDay(); //return me 2(day in week)

但它返回我 2 而不是 21 :/

有人可以帮我吗?

【问题讨论】:

  • 你的意思是date.getDate()??
  • 请注意,“2020-07-21T08:08:20.794Z”是 UTC,但 getDaygetDate 返回可能为 ± 1 取决于时间戳中的时间和特定日期和时间的本地时区偏移量。 :-)

标签: javascript typescript date datetime


【解决方案1】:

date 有一个 getDate 方法,它不同于 getDay 方法。

getDate 返回月份中的某天。 getDay 返回一周中的哪一天。

const date = new Date(`2020-07-21T08:08:20.794Z`);
const dayNumber = date.getDate(); //return me 2(day in week)

【讨论】:

    【解决方案2】:

    我相信你想要date.getDate()(我知道这个名字有点混乱)。这是我得到的:

    asyncify:~ js$ node
    Welcome to Node.js v12.10.0.
    Type ".help" for more information.
    > const date = new Date(`2020-07-21T08:08:20.794Z`);
    undefined
    > date.getDate()
    21
    

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2010-12-19
      相关资源
      最近更新 更多