Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
haizhilin2013 opened this issue Jun 3, 2019 · 7 comments
Open

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

haizhilin2013 opened this issue Jun 3, 2019 · 7 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

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

@haizhilin2013 haizhilin2013 added the js JavaScript label Jun 3, 2019
@wenyejie
Copy link

wenyejie commented Jun 4, 2019

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

@Konata9
Copy link

Konata9 commented Jul 9, 2019

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

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
Copy link

wyx2014 commented Jul 25, 2019

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

@Vi-jay
Copy link

Vi-jay commented Jul 31, 2019

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

@xuwencheng
Copy link

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

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
Copy link

Lhasa23 commented Apr 16, 2020

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

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
Copy link

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
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

8 participants