在react中使用date-fns:
import sub_days from 'date-fns/sub_days';
import start_of_week from 'date-fns/start_of_week';
import end_of_week from 'date-fns/end_of_week';
import start_of_month from 'date-fns/start_of_month';
import end_of_month from 'date-fns/end_of_month';
 
const theMoment = new Date();
const today = +theMoment;
const yerterday = +sub_days(theMoment, 1);
const startOfWeek = +start_of_week(theMoment, { weekStartsOn: 1 });
const endOfWeek = +end_of_week(theMoment, { weekStartsOn: 1 });
const startOfMonth = +start_of_month(theMoment);
const endOfMonth = +end_of_month(theMoment);
 
不使用时间库:
let date = new Date()
let lastDay = new Date(date.getTime() - 24 * 60 * 60 * 1000) //昨天
let currentMonth = date.getMonth()
let nextMonth = ++currentMonth
let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
let oneDay = 1000 * 60 * 60 * 24
//返回date是一周中的某一天
let week = date.getDay()
//返回date是一个月中的某一天
let month = date.getDate()
//减去的天数
let minusDay = week != 0 ? week - 1 : 6
//获得当前周的第一天
let currentWeekFirstDay = new Date(date.getTime() - oneDay * minusDay)
//获得当前周的最后一天
let currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + oneDay * 6)

date-fns时间库的基本使用的更多相关文章

  1. JDK1.8日期时间库学习

    这周在阅读<阿里巴巴Java开发手册>时,在并发处理一节的日期处理中,其强调SimpleDateFormat 是线程不安全的类,一般不要定义为 static 变量,如果 定义为 stati ...

  2. moment.js 时间库

    一.概念:    https://www.cnblogs.com/Jimc/p/10591580.html    或    http://momentjs.cn/(官网) 1.Moment.js是一个 ...

  3. moment太重? 那就试试miment--一个超轻量级的js时间库

    介绍 Miment 是一个轻量级的时间库(打包压缩后只有1K),没有太多的方法,Miment的设计理念就是让你以几乎为零的成本快速上手,无需一遍一遍的撸文档 由来 首先 致敬一下Moment,非常好用 ...

  4. Java8新特性探索之新日期时间库

    一.为什么引入新的日期时间库 Java对日期,日历及时间的处理一直以来都饱受诟病,尤其是它决定将java.util.Date定义为可修改的以及将SimpleDateFormat实现成非线程安全的. 关 ...

  5. JS Date当前时间:获取日期时间方法在各浏览器中的差异

    转自:http://www.feiesoft.com/00047/<script type="text/javascript"> // JS Date当前时间获取方法在 ...

  6. java时间库Joda-Time

    虽然在java8里面有内置的最新的时间库,但是在java8之前的版本所有的时间操作都得自己写,未免有些繁琐,如果我们不自己封装的话可以用Joda-Time这个时间库,下面写下这个库的具体用法. git ...

  7. c++11 Chrono时间库

    c++11 Chrono时间库 http://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=chrono ...

  8. js字符串转日期,js字符串解析成日期,js日期解析, Date.parse小时是8点,Date.parse时间多了8小时

    js字符串转日期,js字符串解析成日期,js日期解析, Date.parse小时是8点,Date.parse时间多了8小时 >>>>>>>>>&g ...

  9. ⏰Day.js 2kB超轻量时间库 和Moment.js一样的API

    Moment.js 是一个大而全的 JS 时间库,很大地方便了我们处理日期和时间.但是 Moment.js太重了(200k+ with locals),可能一般项目也只使用到了她几个常用的API.虽然 ...

  10. js非常强大的日历控件fullcalendar.js, 日期时间库: moment.js

    日历控件: https://fullcalendar.io/docs/ https://fullcalendar.io/docs/event_data/events_function/ https:/ ...

随机推荐

  1. JavaScript使用MQTT

    1.MQTT Server使用EMQTTD开源库,自行安装配置: 2.JS使用Websocket连接通信. 3.JS的MQTT库为paho-mqtt,git地址:https://github.com/ ...

  2. python——面向对象基础(2),烤地瓜

    """Date:2020.2.9 测试案例:烤地瓜需求分析1.烤的时间和对应的地瓜状态:2.烤制过程步骤: 1.定义类, 地瓜属性,状态,烤的时间,调料 2.定义方法,怎 ...

  3. GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)

    GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频) GraphQL + React Apoll ...

  4. 阿里云K8S下玩.NET CORE 3.1

    1. 创建阿里云K8S集群,本文以标准托管集群为例 1.1 创建一个 2台 centos 2core 4G的 k8s 集群 1.2 创建成功的模样 2. 创建 asp.net core webapi项 ...

  5. css基础-float浮动

    float实现文字环绕图片效果: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  6. CF #618 div.2

    序 闲来无事,打场CF,本人蒟蒻,考场A了前三道,第四有解答 正文 T1 Non-zero 是道水题.... 给你一个序列a.要求你输出最少的操作次数使这个序列的累和与累乘都不为0: 一次操作指给\( ...

  7. Wannafly Winter Camp 2020 Day 6J K重排列 - dp

    求 \(K\) 是多少个 \(n\) 元置换的周期.\(T\leq 100, n\leq 50, K \leq 10^{18}\) Solution 置换可以被试做若干个环组成的有向图,于是考虑 dp ...

  8. 方法重载(method overloading)

    为什么需要方法重载? 在编程语言中,名字的使用很重要.创建对象的时候,我们给一块内存区域起一个名字,然后这个名字就是我们创建的对象的引用,只要我们"叫"这个名字,计算机就知道我们在 ...

  9. Rtudio 安装包报错

    今天重新安装了一下Rstudio,基本上很多包都安装不上,问了度娘发现被墙了 f..k.. 解决办法,更改安装包的镜像为清华镜像 tools->gloabl options->packag ...

  10. Docker+Nginx+Tomcat实现负载均衡

    环境检测: 1.Docker没有安装的小伙伴请查看https://www.cnblogs.com/niuniu0108/p/12372531.html 2.没有创建Nginx容器的小伙伴请查看http ...