Skip to content

[js] 第49天 写个还剩下多少天过年的倒计时 #187

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第49天 写个还剩下多少天过年的倒计时

Activity

wenyejie

wenyejie commented on Jun 4, 2019

@wenyejie
const day =  Math.floor((new Date('2019-12-31 23:59:59:999') - new Date()) / 864e5) // 210
Konata9

Konata9 commented on Jul 9, 2019

@Konata9

西历新年好算,顺带增加了小时、周、月的维度。农历就懵了……等大佬答案

const countDown = (range = "day") => {
  const nowDate = new Date();
  const currentYear = nowDate.getFullYear();
  const nextYear = new Date(currentYear + 1, 1, 1);

  const rangeBase = {
    minute: 1000 * 60,
    hour: 1000 * 60 * 60,
    day: 1000 * 60 * 60 * 24,
    week: 1000 * 60 * 60 * 24 * 7,
    month: 1000 * 60 * 60 * 24 * 30
  };

  return Math.floor(
    (nextYear.valueOf() - nowDate.valueOf()) /
      (rangeBase[range] || rangeBase.day)
  );
};

console.log(countDown("hour"));
console.log(countDown());
console.log(countDown("week"));
console.log(countDown("month"));
wyx2014

wyx2014 commented on Jul 25, 2019

@wyx2014

const getLastDays = function () { return Math.floor((new Date('2019-12-31 23:59:59:999') - new Date().getTime())/(24*3600000)); }

Vi-jay

Vi-jay commented on Jul 31, 2019

@Vi-jay
Math.floor((new Date("2019-12-31") - Date.now()) / (10**5 *36*24))
xuwencheng

xuwencheng commented on Jan 6, 2020

@xuwencheng

西历新年好算,顺带增加了小时、周、月的维度。农历就懵了……等大佬答案

const countDown = (range = "day") => {
  const nowDate = new Date();
  const currentYear = nowDate.getFullYear();
  const nextYear = new Date(currentYear + 1, 1, 1);

  const rangeBase = {
    minute: 1000 * 60,
    hour: 1000 * 60 * 60,
    day: 1000 * 60 * 60 * 24,
    week: 1000 * 60 * 60 * 24 * 7,
    month: 1000 * 60 * 60 * 24 * 30
  };

  return Math.floor(
    (nextYear.valueOf() - nowDate.valueOf()) /
      (rangeBase[range] || rangeBase.day)
  );
};

console.log(countDown("hour"));
console.log(countDown());
console.log(countDown("week"));
console.log(countDown("month"));

我也想知道算阴历的话应该怎么算

Lhasa23

Lhasa23 commented on Apr 16, 2020

@Lhasa23

西历新年好算,顺带增加了小时、周、月的维度。农历就懵了……等大佬答案

const countDown = (range = "day") => {
  const nowDate = new Date();
  const currentYear = nowDate.getFullYear();
  const nextYear = new Date(currentYear + 1, 1, 1);

  const rangeBase = {
    minute: 1000 * 60,
    hour: 1000 * 60 * 60,
    day: 1000 * 60 * 60 * 24,
    week: 1000 * 60 * 60 * 24 * 7,
    month: 1000 * 60 * 60 * 24 * 30
  };

  return Math.floor(
    (nextYear.valueOf() - nowDate.valueOf()) /
      (rangeBase[range] || rangeBase.day)
  );
};

console.log(countDown("hour"));
console.log(countDown());
console.log(countDown("week"));
console.log(countDown("month"));

抬个杠,new Date()的第二个参数是monthIndex,取值是0-11

xiaoqiangz

xiaoqiangz commented on Jun 6, 2022

@xiaoqiangz

console.log(Math.floor((new Date('2022-12-31 23:59:59:999') - new Date()) / 86400000))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wenyejie@haizhilin2013@Konata9@wyx2014@xiaoqiangz

        Issue actions

          [js] 第49天 写个还剩下多少天过年的倒计时 · Issue #187 · haizlin/fe-interview