如何在 Javascript 中开始和结束一天?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8636617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:49:53  来源:igfitidea点击:

How to get start and end of day in Javascript?

javascript

提问by Bdfy

How to get start ( 00:00:00 ) and end ( 23:59:59 ) of today in timestamp ( GMT )? Computer use a local time.

如何以时间戳(格林威治标准时间)开始( 00:00:00 )和今天的结束( 23:59:59 )?计算机使用本地时间。

回答by tvanfosson

var start = new Date();
start.setHours(0,0,0,0);

var end = new Date();
end.setHours(23,59,59,999);

alert( start.toUTCString() + ':' + end.toUTCString() );

If you need to get the UTC time from those, you can use UTC().

如果您需要从中获取 UTC 时间,您可以使用UTC().

回答by chridam

Using the momentjslibrary, this can be achieved with the startOf()and endOf()methods on the moment's current date object, passing the string 'day'as arguments:

使用momentjs库,这可以通过当前日期对象上的startOf()endOf()方法实现,将字符串'day'作为参数传递:

Local GMT:

当地格林威治标准时间:

var start = moment().startOf('day'); // set to 12:00 am today
var end = moment().endOf('day'); // set to 23:59 pm today

For UTC:

对于 UTC:

var start = moment.utc().startOf('day'); 
var end = moment.utc().endOf('day'); 

回答by tk120404

Using the luxon.jslibrary, same can be achieved using startOf and endOf methods by passing the 'day' as parameter

使用luxon.js库,同样可以使用 startOf 和 endOf 方法通过传递“day”作为参数来实现

var DateTime = luxon.DateTime;
DateTime.local().startOf('day').toUTC().toISO(); //2017-11-16T18:30:00.000Z
DateTime.local().endOf('day').toUTC().toISO(); //2017-11-17T18:29:59.999Z
DateTime.fromISO(new Date().toISOString()).startOf('day').toUTC().toISO(); //2017-11-16T18:30:00.000Z

remove .toUTC() if you need only the local time

如果您只需要本地时间,请删除 .toUTC()

and you may ask why not moment.js, answer is herefor that.

你可能会问为什么不是moment.js,答案就在这里

回答by afinemonkey

If you're just interested in timestamps in GMTyou can also do this, which can be conveniently adapted for different intervals (hour: 1000 * 60 * 60, 12 hours: 1000 * 60 * 60 * 12, etc.)

如果您只对GMT中的时间戳感兴趣,您也可以这样做,它可以方便地适应不同的时间间隔(小时:1000 * 60 * 60、12 小时:1000 * 60 * 60 * 12等)

const interval = 1000 * 60 * 60 * 24; // 24 hours in milliseconds

let startOfDay = Math.floor(Date.now() / interval) * interval;
let endOfDay = startOfDay + interval - 1; // 23:59:59:9999

回答by Ashutosh Jha

In MomentJsWe can declare it like :

MomentJs我们可以这样声明:

   const start = moment().format('YYYY-MM-DD 00:00:01');
   const end = moment().format('YYYY-MM-DD 23:59:59');

回答by Musa

FYI (merged version of Tvanfosson)

仅供参考(Tvanfosson 的合并版本)

it will return actual date => date when you are calling function

当您调用函数时,它将返回实际日期 => 日期

export const today = {
  iso: {
    start: () => new Date(new Date().setHours(0, 0, 0, 0)).toISOString(),
    now: () => new Date().toISOString(),
    end: () => new Date(new Date().setHours(23, 59, 59, 999)).toISOString()
  },
  local: {
  start: () => new Date(new Date(new Date().setHours(0, 0, 0, 0)).toString().split('GMT')[0] + ' UTC').toISOString(),
  now: () => new Date(new Date().toString().split('GMT')[0] + ' UTC').toISOString(),
  end: () => new Date(new Date(new Date().setHours(23, 59, 59, 999)).toString().split('GMT')[0] + ' UTC').toISOString()
  }
}

// how to use

// 如何使用

today.local.now(); //"2018-09-07T01:48:48.000Z" BAKU +04:00
today.iso.now(); // "2018-09-06T21:49:00.304Z" * 

* it is applicable for Instant time type on Java8 which convert your local time automatically depending on your region.(if you are planning write global app)

* 适用于 Java8 上的 Instant time 类型,它会根据您所在的地区自动转换您的当地时间。(如果您打算编写全局应用程序)

回答by Mikhail Shabrikov

I prefer to use date-fnslibrary for date manipulating. It is really great modular and consistent tool. You can get start and end of the day this way:

我更喜欢使用date-fns库进行日期操作。它真的是很棒的模块化和一致的工具。您可以通过以下方式开始和结束一天:

var startOfDay = dateFns.startOfDay;
var endOfDay = dateFns.endOfDay;

console.log('start of day ==> ', startOfDay(new Date('2015-11-11')));
console.log('end of day ==> ', endOfDay(new Date('2015-11-11')));
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>