关于date:用js获取当前月份的天数

本文介绍一下如何应用js获取指定工夫对应月份的天数。获取以后月份天数我测试的工夫是2022-09-01: const date = new Date()const year = date.getFullYear()const month = date.getMonth()const days = new Date(year,month+1,0).getDate() // 30如果要获取2022-02的天数: const days = new Date(2022,2,0).getDate() // 28留神:new Date()接管的第三个参数是0,第二个参数是人类意识中的月份(因为date.getMonth()失去的值比设想中的小1) 补充月份是从0开始计算的: new Date('2022-01-01 13:55:33').getMonth()// 0获取指定日期 是 星期几: new Date('2022-08-28 13:55:33').getDay()// 0 星期日(老外喜爱把一周中的星期日当成第一天,也是从0开始) 利用场景我是在应用 echarts 时遇到了这个问题,须要按月份生成数据: 具备了下面的基础知识,就能够搞定这个问题了: function genDaysArr(timestamp) { const d = new Date(timestamp) const y = d.getFullYear() const m = d.getMonth()+1 const m_str = m>10?m:'0'+m // 获取指定月份天数 const days = new Date(y,m,0).getDate() const arr = [] for (let i = 1; i <= days; i ++) { const day_str = i>=10?i:'0'+i arr.push({day: `${y}-${m_str}-${day_str}`, count: 0}) } return arr}const a = genDaysArr(1647852283000)console.log(a)/**[ { day: '2022-03-01', count: 0 }, { day: '2022-03-02', count: 0 }, { day: '2022-03-03', count: 0 }, { day: '2022-03-04', count: 0 }, { day: '2022-03-05', count: 0 }, { day: '2022-03-06', count: 0 }, { day: '2022-03-07', count: 0 }, { day: '2022-03-08', count: 0 }, { day: '2022-03-09', count: 0 }, { day: '2022-03-10', count: 0 }, { day: '2022-03-11', count: 0 }, { day: '2022-03-12', count: 0 }, { day: '2022-03-13', count: 0 }, { day: '2022-03-14', count: 0 }, { day: '2022-03-15', count: 0 }, { day: '2022-03-16', count: 0 }, { day: '2022-03-17', count: 0 }, { day: '2022-03-18', count: 0 }, { day: '2022-03-19', count: 0 }, { day: '2022-03-20', count: 0 }, { day: '2022-03-21', count: 0 }, { day: '2022-03-22', count: 0 }, { day: '2022-03-23', count: 0 }, { day: '2022-03-24', count: 0 }, { day: '2022-03-25', count: 0 }, { day: '2022-03-26', count: 0 }, { day: '2022-03-27', count: 0 }, { day: '2022-03-28', count: 0 }, { day: '2022-03-29', count: 0 }, { day: '2022-03-30', count: 0 }, { day: '2022-03-31', count: 0 }] */只有传入对应月份的工夫戳就能够生成这个月的根底数据了。 ...

September 1, 2022 · 2 min · jiezi

关于date:不同数据库模式下DATE类型的行为解析

摘要:本文章次要介绍了GaussDB(DWS)数据类型中的DATE类型在不同数据库模式下且在不同利用场景下的行为表现及比照。本文分享自华为云社区《GaussDB(DWS)数据类型之DATE类型》,原文作者:小仲。 GaussDB(DWS)中有三种模式,别离是Oracle模式,TD模式和MySQL模式。不同的类型在不同的模式下,也有不同的行为表现。接下来便对GaussDB(DWS)中的日期DATE类型进行具体介绍。 DATE类型的取值范畴:GaussDB(DWS)在存储DATE类型时,不反对年月日全副为零或局部为零的场景,且取值范畴在三种模式下是不同的: 在Oracle模式下,DATE类型的取值范畴是公元前4713年1月1日到公元294277年1月1日。在MySQL模式和TD模式中,DATE类型的取值范畴是公元前4713年到公元5874897年。 用例阐明:1. 不同模式下DATE类型的行为Oracle模式a. DATE类型的体现 GaussDB(DWS)中DATE类型存储时不带时分秒,因为在Oracle模式下的DATE类型须要带有时分秒,使得GaussDB(DWS)在Oracle模式下存在带时分秒的和不带时分秒的两种DATE类型,所以为了容易辨别,在显示时不带时分秒的DATE类型用列名date示意;带时分秒的DATE类型用同样带时分秒的TIMESTAMP类型的列名timestamp示意。 postgres=# select date '4713-01-01 BC'; timestamp ------------------------ 4713-01-01 00:00:00 BC(1 row)postgres=# select date '294277-01-01'; timestamp ----------------------- 294277-01-01 00:00:00(1 row)b. Oracle模式下失去只含有年月日的DATE类型的办法 如果想要在Oracle模式下将日期转成只有年月日的DATE类型,有办法能够实现么?事实上也是有的。GaussDB(DWS)提供了两种形式,一种是通过在Oracle模式下应用date函数:date函数的参数至多须要蕴含年月日,并将日期中的年月日提取并输入;另一种则是通过cast函数进行转换,但转换时应将date加上双引号,否则转换后的数据还是会含有时分秒局部。 postgres=# select date('2008-05-24 10:40:21'); date ------------ 2008-05-24(1 row)postgres=# select cast('2008-05-24 10:40:21.100050' as "date"); date ------------ 2008-05-24(1 row)postgres=# select cast('2008-05-24 10:40:21.100050' as date); timestamp --------------------- 2008-05-24 10:40:21(1 row)c. DATE类型操作符 MySQL模式和TD模式: DATE类型只有年月日。a. DATE类型的体现 MySQL模式与TD模式下DATE类型无差别。 mysql_db=# select date '5874897-01-01'; date --------------- 5874897-01-01(1 row)td_db=# select date '5874897-01-01'; date --------------- 5874897-01-01(1 row)b. MySQL模式与TD模式下失去只含有年月日的DATE类型的办法 ...

May 26, 2021 · 1 min · jiezi

JavaScript如何获取某一天所在的星期

我们会遇到的需求的是,获取今天或者某一天所在星期的开始和结束日期。 我们这里来获取今天所在星期的始末日期,我们可以通过(new Date).getDay()来获取今天是星期几,然后再通过这个减去或者加上一定的天数,就是这个星期的开始日期和结束日期。 function getWeekStartAndEnd() { const oneDayTime = 1000 * 60 * 60 * 24; // 一天里一共的毫秒数 const today = new Date(); const todayDay = today.getDay(); // 获取今天是星期几,假设是周3 const startDate = new Date( today.getTime() - oneDayTime * (todayDay - 1) ); const endDate = new Date(today.getTime() + oneDayTime * (7 - todayDay)); return { startDate, endDate };}const week = getWeekStartAndEnd();console.log(week.startDate, week.endDate);是不是很完美?但,这里有一个很大的 bug!注意:如果今天是周日,那么todayDay就会是 0,若还是按照上面的思路,则星期一的日期会变成下周一的日期,星期日的日期会变成下周日的日期。因此,这里我们需要特殊处理下,当 todayDay 为 0 时,就将其赋值为 7。同时,我们还可以传入一个时间戳,获取特定某一天所在的星期。 ...

September 10, 2019 · 1 min · jiezi

php-学习笔记之日期时间操作一箩筐

格式化日期时间date : 格式化日期时间场景将当前日期时间或者特定日期时间格式化输出为特定格式的字符串,常用于人性化展示信息. 说明返回给定时间戳格式化后所产生的日期时间字符串,如果没有给出时间戳则默认使用本地当前时间. 备注格式说明返回值示例Y4 位数字完整表示的年份2019y2 位数字表示的年份19M三个字母缩写表示的月份Jan 到 Decm数字表示的月份,有前导零01 到 12D星期中的第几天,文本表示,3个字母Mon 到 Sund月份中的第几天,有前导零的 2 位数字01 到 31H小时,24 小时格式,有前导零00 到 23h小时,12 小时格式,有前导零01 到 12I是否为夏令时如果是夏令时为1 ,否则为 0i有前导零的分钟数00 到 59S每月天数后面的英文后缀,2 个字符st,nd,rd 或者 th ,可以和 j 一起用s秒数,有前导零00 到 59常用格式// 形如 2019-05-31 12:00:00echo date("Y-m-d H:i:s");// 形如 2019/05/31 12:00:00echo date("Y/m/d H:i:s");// 形如 2019年05月31日 12时00分00秒echo date("Y年m月d日 H时i分s秒");示例<?php// 设置当前时区为上海时区date_default_timezone_set("Asia/Shanghai");// 获取当前时区 : Asia/Shanghaiecho "当前时区 : ".date_default_timezone_get()."<br/>";// `Y年m月d日 H时i分s秒` 格式化当前时间 : 2019年05月30日 22时32分46秒echo "当前时间 : ".date("Y年m月d日 H时i分s秒")."<br/>";// `Y-m-d H:i:s` 格式化当前时间 : 2019-05-30 22:32:46echo "当前时间 : ".date("Y-m-d H:i:s")."<br/>";// `w` 星期中的第几天,数字表示: 0(表示星期天)到 6(表示星期六)switch (date("w")) { case '0': $dayStr = "日"; break; case '1': $dayStr = "一"; break; case '2': $dayStr = "二"; break; case '3': $dayStr = "三"; break; case '4': $dayStr = "四"; break; case '5': $dayStr = "五"; break; case '6': $dayStr = "六"; break; default: $dayStr = "未知"; break;} // 2019年05月30日 星期四echo "当前时间 : ".date("Y年m月d日")." 星期".$dayStr."<br/>";echo "<hr/>";// `z` 年份中的第几天 : 今天是全年的第149天echo "今天是全年的第".date("z")."天<br/>";// `W` ISO-8601 格式年份中的第几周,每周从星期一开始 : 本周是全年的第22周echo "本周是全年的第".date("W")."周<br/>";// `t` 指定的月份有几天 : 本月共有31天echo "本月共有".date("t")."天<br/>";?>日期转化时间戳time : 返回当前的 Unix 时间戳场景获取当前日期时间或特定日期时间的时间戳,常用于日期时间之间的相互转换. ...

May 31, 2019 · 2 min · jiezi

php-学习笔记之关于时区的那点事

科普一下什么是时区众所周知,地球绕着太阳转的同时也会自转,因此同一时刻不同地区所接收到太阳照射的情况不同,所以有的地区是日出,有的地区是日落,还有的地区可能是黑夜. 既然地球上的不同地区时间不同,那总要有统一的时间刻度才能方便文化科技交流吧,不然大家说的都是当地时间,这样岂不是乱套了? 有问题就要解决问题,不同地区时间不同就要统一时间标准,而统一时间标准的前提就是要弄清楚全球的时间差异到底在哪以及各地的当地时间如何互相转换. 原来的时间标准是格林尼治标准时间,随着精确计时的发展需要,已被新的时间标准所取代,目前的时间标准是世界协调时. 现在有了统一的时间标准,不同地区的时间就可以统一换算成世界协调时再转换成当地时间了,再也不会出现同一时刻不同时间了! 示例: 北京时间(UTC+8) : 2019-05-30 13:30:00世界时间(UTC) : 2019-05-30 05:30:00东部时间(UTC-5) : 2019-05-30 00:30:00格林尼治标准时间格林尼治标准时间(又称格林威治平均时间或格林威治标准时间,英文是GreenwichMeanTime,简称GMT ),格林尼治标准时间的正午是指当太阳横穿格林尼治子午线时(也就是在格林尼治时)的时间.格林尼治是英国伦敦的一个小镇,是地理经度的起点,本初子午线所在的经度是零度经度,所在的时区是零时区. 我们知道一天共有 24 小时,对应着全球 24 个时区,而地球自西向东自转,零时区后依次是东一区到东十一区,然后是东十二区.紧接着是西十二区,西十一区到西一区,最后又回到零时区. 其中东十二区和西十二区是同一个时区,又被称为东西十二区.总的来说,时区分为东十二区和西十二区以及零时区,其中东西十二区是同一个时区,因此共有 24 个时区. 示例: 由于北京位于东八区,比零时区多个 8 个时区,意味着北京时间比格林标准时间快 8 个小时. 所以,北京时间中午 12 点整的时候,格林尼治才清早 4 点钟,估计还在睡觉呢! ( GMT+8 就是北京时间) 格林尼治标准时间与地球自转有关,不能满足精确计时的需求,因此不再作为标准时间,取而代之的是协调世界时.协调世界时协调世界时(又称世界统一时间或世界标准时间或国际协调时间,英文是Coordinated Universal Time,简称UTC ),协调世界时是以原子时秒长为基础,在时刻上尽量接近于格林尼治标准时间的一种时间计量系统.在不需要精确到秒的情况下, GMT 和 UTC 基本一致,但 UTC 是以更加精确的原子时为基础,因此常用于科学计算领域,也是目前时间计量的统一标准. 示例: 北京时间 12:00 ,换算成 GMT 或 UTC 时间都是 04:00 ( UTC+8 也是北京时间) 北京时间北京时间(又称中国标准时间),是首都北京所在的时区作为中国的标准时间,比格林尼治标准时间快8小时.我国幅员辽阔,从西到东横跨东五,东六,东七,东八和东九等五个时区.所以全国统一采用首都北京所在的东八时区的区时作为标准时间,也就是北京时间. 时区信息数据库时区信息数据库,又称 Olson数据库,是一个主要应用于电脑程序以及操作系统的可协作编辑世界时区信息的数据库.时区信息数据库采用按“区域/位置”命名规范,方便应用于计算机世界,其中英文地名中的空格用下划线“_”代替,连词符“-”只在英文地名本身包含时使用. 示例: ...

May 30, 2019 · 2 min · jiezi

ionic3-时间选择器

1、安装 npm install ion-multi-picker --save2、导入模块 import { MultiPickerModule } from 'ion-multi-picker';@NgModule({ imports: [ IonicModule.forRoot(MyApp), **MultiPickerModule** ]3、html文件中引入 <ion-multi-picker item-content [multiPickerColumns]="heightChoose" [(ngModel)]="defaultHeight" (ionChange)="historyPickerChange($event)" [cancelText]="'cancel'" [doneText]="'done'" [resetText]="'reset'" [showReset]="false"></ion-multi-picker>4、数据 constructor() { this.heightChoose = [ { options: [ {text: "0", value: "0"}, {text: "1", value: "1"}, {text: "2", value: "2"}, {text: "3", value: "3"}, {text: "4", value: "4"}, {text: "5", value: "5"}, {text: "6", value: "6"}, {text: "7", value: "7"}, {text: "8", value: "8"}, {text: "9", value: "9"}, {text: "10", value: "10"}, ] }, { options: [ {text: "00", value: "0"}, {text: "01", value: "1"}, {text: "02", value: "2"}, {text: "03", value: "3"}, {text: "04", value: "4"}, {text: "05", value: "5"}, {text: "06", value: "6"}, {text: "07", value: "7"}, {text: "08", value: "8"}, {text: "09", value: "9"}, {text: "10", value: "10"}, // {text: "1-1", value: "1-1", parentVal: "1"}, 指定父级 // {text: "1-2", value: "1-2", parentVal: "2"}, ] }, { options: [ {text: "am", value: "0"}, {text: "pm", value: "1"}, ] } ]; }

May 7, 2019 · 1 min · jiezi

乐字节-Java8新特性之Date API

上一篇文章,小乐给大家带来了Java8新特性之Optional,接下来本文将会给大家介绍Java8新特性之Date API前言:Java 8通过发布新的Date-Time API来进一步加强对日期与时间的处理。 旧版的 Java 中,日期时间 API 存在诸多问题 :非线程安全 − java.util.Date 是非线程安全的,所有的日期类都是可变的,设计很差 − Java的日期/时间类的定义并不一致,在java.util和java.sql的包中都有日期类,此外用于格式化和解析的类在java.text包中定义。时区处理麻烦 − 日期类并不提供国际化,没有时区支持Java 8 在 java.time 包下提供了很多新的 API。以下为两个比较重要的 API:Local(本地) : 简化了日期时间的处理,没有时区的问题。Zoned(时区) − 通过制定的时区处理日期时间。新的java.time包涵盖了所有处理日期,时间,日期/时间,时区,时刻(instants),过程(during)与时钟(clock)的操作。1、LocalDateTimeLocalDateTime ldt = LocalDateTime.now();// 获取系统当前时间System.out.println(ldt);LocalDateTime ldt2 = LocalDateTime.of(2019, 01, 01, 12, 12, 12, 888000000);// 构建LocalDateTime对象ldtSystem.out.println(ldt2);// 获取明年此时的时间LocalDateTime ldt3 = ldt.plusYears(1);System.out.println(ldt3);// 获取去年此刻时间LocalDateTime ldt4 = ldt.minusYears(1);System.out.println(ldt4);// 获取年System.out.println(ldt.getYear());// 获取月份System.out.println(ldt.getMonthValue());// 获取本月第某天System.out.println(ldt.getDayOfMonth());// 获取时System.out.println(ldt.getHour());// 获取分System.out.println(ldt.getMinute());// 获取秒System.out.println(ldt.getSecond());// 获取纳秒System.out.println(ldt.getNano());2、时间戳时间戳:以Unix元年:1970年1月1日 00:00:00 至目前时间之间的毫秒值 public static void testInstant(){ // 时间戳 Instant Instant ins1 = Instant.now(); // 默认获取UTC时间,协调世界时 System.out.println(ins1); OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8)); System.out.println(odt); System.out.println(ins1.toEpochMilli()); Instant ins2 = Instant.ofEpochSecond(60); System.out.println(ins2);}3、日期时间间隔计算:Duration、Periodpublic static void testDuration(String[] args) { Instant ins1 = Instant.now(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } Instant ins2 = Instant.now(); Duration dura = Duration.between(ins1, ins2); System.out.println(dura.toMillis()); System.out.println("———————-"); LocalTime lt1 = LocalTime.now(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } LocalTime lt2 = LocalTime.now(); Duration dura2 = Duration.between(lt1, lt2); System.out.println(dura2.toMillis()); } public static void testPeriod(){ LocalDate ld1 = LocalDate.of(2015, 2, 2); LocalDate ld2 = LocalDate.now(); Period period = Period.between(ld1, ld2); System.out.println(period); System.out.println(“相差” + period.getYears() + “年” + period.getMonths() + “月” + period.getDays() + “天”); }4、时间校正:TemporalAdjusterpublic static void testTemporalAdjuster(){ LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt); LocalDateTime ldt2 = ldt.withDayOfMonth(10); System.out.println(ldt2); LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SATURDAY)); System.out.println(ldt3);}5、日期时间格式化public static void testDateFormate(){ DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE; LocalDateTime ldt = LocalDateTime.now(); String strDate = ldt.format(dtf); System.out.println(strDate); System.out.println("—————————-"); DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern(“yyyy年MM月dd日 HH:mm:ss”); String strDate2 = dtf2.format(ldt); System.out.println(strDate2); System.out.println("—————————–"); LocalDateTime newDate = ldt.parse(strDate2, dtf2); System.out.println(newDate); }6、TimeZone 时区处理 // 时区的处理 public static void testTimeZone(){ LocalDateTime ldt = LocalDateTime.now(ZoneId.of(“Europe/Dublin”)); System.out.println(ldt); LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of(“Europe/Dublin”)); ZonedDateTime zdt = ldt2.atZone(ZoneId.of(“Europe/Dublin”)); System.out.println(zdt); }这次就分享到这里了,后面小乐会继续给大家介绍Java8新特性的,请大家继续多多关注哦!乐字节只讲Java技术干货。 ...

April 13, 2019 · 2 min · jiezi

JavaScript中Date对象学习记录_013

JavaScript中Date对象学习记录Date 实例用来处理日期和时间。Date对象基于1970年1月1日(世界标准时间)起的毫秒数。JavaScript 的Date对象提供了数个UTC时间的方法,也相应提供了当地时间的方法。UTC,也就是我们所说的格林威治时间,指的是time中的世界时间标准。而当地时间则是指执行JavaScript的客户端电脑所设置的时间。Date 构造函数new Date();//Sun Jan 06 2019 20:18:04 GMT+0800 (中国标准时间)new Date(value); //value 代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数。new Date(000000000000);//Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)new Date(dateString);//dateString表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别new Date(“2019.01.01”);//Tue Jan 01 2019 00:00:00 GMT+0800 (中国标准时间)new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);//year代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要用 98.//month代表月份的整数值从0(1月)到11(12月)。//day代表一个月中的第几天的整数值,从1开始。//hour代表一天中的小时数的整数值 (24小时制)。// minute分钟数。// second秒数。//millisecond表示时间的毫秒部分的整数值。new Date(2019,01,01,01,01,01);//Fri Feb 01 2019 01:01:01 GMT+0800 (中国标准时间)Date方法Date.now()返回自 1970-1-1 00:00:00 UTC (世界标准时间)至今所经过的毫秒数,类型为Number。Date.now()//1546777708417Date.parse()解析一个表示日期的字符串,并返回从 1970-1-1 00:00:00 所经过的毫秒数。Date.parse(“2019.01.01”)//1546272000000Date.parse(‘01 Jan 1970 00:00:00 GMT’);//0Date.UTC()接受和构造函数最长形式的参数相同的参数(从2到7),并返回从 1970-01-01 00:00:00 UTC 开始所经过的毫秒数。year:1900 年后的某一年份。month:0 到 11 之间的一个整数,表示月份。date:1 到 31 之间的一个整数,表示某月当中的第几天。hrs:0 到 23 之间的一个整数,表示小时。min:0 到 59 之间的一个整数,表示分钟。sec 0 到 59 之间的一个整数,表示秒。ms0 到 999 之间的一个整数,表示毫秒。new Date(Date.UTC(2019, 0, 0, 0, 0, 1));//Mon Dec 31 2018 08:00:01 GMT+0800 (中国标准时间)时间戳格式转换 dateFormmat(time) { let date = new Date(time * 1000); //如果date为13位不需要乘1000 let Ye = date.getFullYear() + ‘/’; let Mo = (date.getMonth() + 1 < 10 ? ‘0’ + (date.getMonth() + 1) : date.getMonth() + 1) + ‘/’; let Da = (date.getDate() < 10 ? ‘0’ + date.getDate() : date.getDate()) + ’ ‘; let hh = (date.getHours() < 10 ? ‘0’ + date.getHours() : date.getHours()) + ‘:’; let mm = (date.getMinutes() < 10 ? ‘0’ + date.getMinutes() : date.getMinutes()) + ‘:’; let ss = date.getSeconds() < 10 ? ‘0’ + date.getSeconds() : date.getSeconds(); return Ye + Mo + Da +hh + mm + ss } //let value=dateFormmat(1234567890)//console.log(value)//2009/02/14 07:31:30Date 实例-(get)所有的 Date 实例都继承自 Date.prototype。修改 Date 构造函数的原型对象会影响到所有的 Date 实例。Date.getDate()根据本地时间,返回一个指定的日期对象为一个月中的第几天。getDate() 返回一个1 到 31的整数值let date = new Date(“December 25, 2019 11:11:00”);let day = date.getDate();console.log(day)//25Date.getDay()getDay() 返回一个整数值: 0 代表星期日, 1 代表星期一,2 代表星期二, 依次类推Date.getFullYear()getFullYear() 方法根据本地时间返回指定日期的年份。Date.getMonth()根据本地时间,返回一个指定的日期对象的月份,为基于0的值(0表示一年中的第一月)。Date.getHours()getHours() 方法根据本地时间,返回一个指定的日期对象的小时。getHours返回一个0 到 23之间的整数值。Date.getMinutes()getMinutes() 方法根据本地时间,返回一个指定的日期对象的分钟数。getMinutes 返回一个0 到 59的整数值Date.getSeconds()getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数,返回一个 0 到 59 的整数值。Date.getMilliseconds()getMilliseconds() 方法,根据本地时间,返回一个指定的日期对象的毫秒数。getMilliseconds() 方法返回一个0 到 999的整数。Date.getTime()getTime 方法的返回值一个数值,表示从1970年1月1日0时0分0秒(UTC,即协调世界时)距离该日期对象所代表时间的毫秒数。Date 实例-(set)Date.setDate()setDate() 方法根据本地时间来指定一个日期对象的天数。如果 dayValue 超出了月份的合理范围,setDate 将会相应地更新 Date 对象。例如,如果为 dayValue 指定0,那么日期就会被设置为上个月的最后一天。Date.setFullYear()setFullYear() 方法根据本地时间为一个日期对象设置年份如果有一个参数超出了合理的范围,setFullYear 方法会更新其他参数值,日期对象的日期值也会被相应更新。 例如,为 monthValue 指定 15, 则年份会加1,月份值会为3。Date.setHours()setHours() 方法根据本地时间为一个日期对象设置小时数,返回从1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。如果有一个参数超出了合理范围,setHours 会相应地更新日期对象中的日期信息。例如,如果为 secondsValue 指定了 100,则分钟会加 1,然后秒数使用 40。Date.setMilliseconds()setMilliseconds() 方法会根据本地时间设置一个日期对象的豪秒数。如果指定的数字超出了合理范围,则日期对象的时间信息会被相应地更新。例如,如果指定了 1005,则秒数加 1,豪秒数为 5。Date.setMinutes()setMinutes() 方法根据本地时间为一个日期对象设置分钟数。如果有一个指定的参数超出了合理范围,setMinutes 会相应地更新日期对象中的时间信息。例如,为 secondsValue 指定 100,分钟数将会加 1,而秒数会为 40。Date.setMonth()setMonth() 方法根据本地时间为一个设置年份的日期对象设置月份如果有一个指定的参数超出了合理范围,setMonth 会相应地更新日期对象中的日期信息。例如,为 monthValue 指定 15,则年份会加 1,月份将会使用 3。Date.setSeconds()setSeconds() 方法根据本地时间设置一个日期对象的秒数。如果一个参数超出了合理范围, setSeconds 方法会相应地更新日期对象的时间信息。例如,为 secondsValue 指定 100,则日期对象的分钟数会相应地加 1,秒数将会使用 40。Date.setTime()setTime() 方法以一个表示从1970-1-1 00:00:00 UTC计时的毫秒数为来为 Date 对象设置时间。https://developer.mozilla.org… ...

January 6, 2019 · 2 min · jiezi

getDate方法的妙用(js判断闰年)

对于js中的Date对象,我们new Date()后做的最多的操作就是getTime()、getFullYear()、getMonth()、getSecond(),在实际开发中几乎很少会用到getDate()这个方法,因为应用场景太少了。在工作中我们经常会需要判断某个年份是否是闰年这个需求,以前我们都是使用一套公式算出来的,这个方法比较麻烦,并且公式还记不住。getDate()方法就是我们的福音,用它可以很方便的实现判断闰年的需求!1.1、getDate()方法的使用new Date()方法可以传递3个参数,如:new Date(年,月,数字);1、new Date()第三个参数为0或为负数/* 28,表示获取2018年2月份的最后一天,即获取2018年2月份的倒数第一天。/new Date(2018,2,0); // 28new Date(2018,2,-1); // 27new Date(2018,2,-2); // 26new Date(2018,4,0); // 30new Date(2020,2,0); // 291.2、new Date()第三个参数为正数 当第三个参数为正数时表示获取该数字在(月份+1)中的第几天,如果数字大于(月份+1)的最大天数,则月份往下再加1,然后再进行计算。/ 25,表示获取2018年4月份25日在当月中的序号 /new Date(2018,3,25); // 25/ 1,因为4月份没有31号,因此月份再加1,序号变为31-30(30为4月份最大天数),即获取的是5月份1号在5月份中的序号 /new Date(2108,3,31); // 1new Date(2108,3,32); // 22、判断年份是否是闰年2.1、使用getDate()方法判断年份是否为闰年/ 当new Date()第三个参数为0时可以获取2018年2月份的最后一天,如果2月份的最后一天是29,那么该年就是闰年 */var days = new Date(2018,2,0); // 28if(days == 29){ console.log(“2018年是闰年”);}else{ console.log(“2018年不是闰年”);}2.2、使用公式计算是否是闰年var year = new Date().getFullYear();if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ console.log(year + “年是闰年”);}else{ console.log(year + “年不是闰年”);} ...

January 3, 2019 · 1 min · jiezi

javascript的date操作使用

DateDate 对象用于处理日期与时间。构造方法new Date();new Date(value);new Date(dateString);new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);参数:value代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数。dateString表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别(符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。year代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要用 98.month代表月份的整数值从0(1月)到11(12月)。day代表一个月中的第几天的整数值,从1开始。hour代表一天中的小时数的整数值 (24小时制)。minute分钟数。second秒数。millisecond表示时间的毫秒部分的整数值。需要注意的是只能通过调用 Date 构造函数来实例化日期对象:以常规函数调用它(即不加 new 操作符)将会返回一个字符串,而不是一个日期对象。另外,不像其他JavaScript 类型,Date 对象没有字面量格式。当Date作为构造函数调用并传入多个参数时,如果数值大于合理范围时(如月份为13或者分钟数为70),相邻的数值会被调整。比如 new Date(2013, 13, 1)等于new Date(2014, 1, 1),它们都表示日期2014-02-01(注意月份是从0开始的)。其他数值也是类似,new Date(2013, 2, 1, 0, 70)等于new Date(2013, 2, 1, 1, 10),都表示时间2013-03-01T01:10:00。如果没有输入任何参数,则Date的构造器会依据系统设置的当前时间来创建一个Date对象。如果提供了至少两个参数,其余的参数均会默认设置为1(如果没有提供day参数)或者0。JavaScript的时间是由世界标准时间(UTC)1970年1月1日开始,用毫秒计时,一天由86,400,000毫秒组成。Date对象的范围是-100,000,000天至100,000,000天(等效的毫秒值)。JavaScript的Date对象为跨平台提供了统一的行为。时间属性可以在不同的系统中表示相同的时刻,而如果使用了本地时间对象,则反映当地的时间。JavaScript 的Date对象提供了数个UTC时间的方法,也相应提供了当地时间的方法。UTC,也就是我们所说的格林威治时间,指的是time中的世界时间标准。而当地时间则是指执行JavaScript的客户端电脑所设置的时间。以一个函数的形式来调用JavaScript的Date对象(i.e., 不使用 new 操作符)会返回一个代表当前日期和时间的字符串。dateString表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别(符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。所以需要确认datestring的类型。IETF-compliant RFC 2822 timestamps最初的datestring的只支持:时间字符串Date and Time Specification Date and time occur in several header fields. This section specifies the syntax for a full date and time specification. Though folding white space is permitted throughout the date-time specification, it is RECOMMENDED that a single space be used in each place that FWS appears (whether it is required or optional); some older implementations may not interpret other occurrences of folding white space correctly.date-time = [ day-of-week “,” ] date FWS time [CFWS]day-of-week = ([FWS] day-name) / obs-day-of-weekday-name = “Mon” / “Tue” / “Wed” / “Thu” / “Fri” / “Sat” / “Sun"date = day month yearyear = 4DIGIT / obs-yearmonth = (FWS month-name FWS) / obs-monthmonth-name = “Jan” / “Feb” / “Mar” / “Apr” / “May” / “Jun” / “Jul” / “Aug” / “Sep” / “Oct” / “Nov” / “Dec"day = ([FWS] 12DIGIT) / obs-daytime = time-of-day FWS zonetime-of-day = hour “:” minute [ “:” second ]hour = 2DIGIT / obs-hourminute = 2DIGIT / obs-minutesecond = 2DIGIT / obs-secondzone = (( “+” / “-” ) 4DIGIT) / obs-zoneThe day is the numeric day of the month. The year is any numeric year 1900 or later. The time-of-day specifies the number of hours, minutes, and optionally seconds since midnight of the date indicated. The date and time-of-day SHOULD express local time. The zone specifies the offset from Coordinated Universal Time (UTC, formerly referred to as “Greenwich Mean Time”) that the date and time-of-day represent. The “+” or “-” indicates whether the time-of-day is ahead of (i.e., east of) or behind (i.e., west of) Universal Time. The first two digits indicate the number of hours difference from Universal Time, and the last two digits indicate the number of minutes difference from Universal Time. (Hence, +hhmm means +(hh * 60 + mm) minutes, and -hhmm means -(hh * 60 + mm) minutes). The form “+0000” SHOULD be used to indicate a time zone at Universal Time. Though “-0000” also indicates Universal Time, it is used to indicate that the time was generated on a system that may be in a local time zone other than Universal Time and therefore indicates that the date-time contains no information about the local time zone. A date-time specification MUST be semantically valid. That is, the day-of-the-week (if included) MUST be the day implied by the date, the numeric day-of-month MUST be between 1 and the number of days allowed for the specified month (in the specified year), the time-of-day MUST be in the range 00:00:00 through 23:59:60 (the number of seconds allowing for a leap second; see [STD12]), and the zone MUST be within the range -9959 through +9959.所以可以得出结论:最开始的js仅支持以下格式的datestring【需要注意的是各个浏览器实现不一致,所以可能会支持其他的格式的datestring】Sun 30 Dec 2018 19:42:44 +0800看起来和console.log里面的输出有一点不一致;Sun Dec 30 2018 19:42:44 GMT+0800 (中国标准时间)version of ISO8601在es5开始,添加了ISO8601格式的支持;Date Time String FormatECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZWhere the fields are as follows:YYYY is the decimal digits of the year 0000 to 9999 in the Gregorian calendar.- “-” (hyphen) appears literally twice in the string.MM is the month of the year from 01 (January) to 12 (December).DD is the day of the month from 01 to 31.T “T” appears literally in the string, to indicate the beginning of the time element.HH is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24.: “:” (colon) appears literally twice in the string.mm is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59.ss is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59.. “.” (dot) appears literally in the string.sss is the number of complete milliseconds since the start of the second as three decimal digits.Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression HH:mmThis format includes date-only forms:YYYYYYYY-MMYYYY-MM-DDIt also includes “date-time” forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional time zone offset appended:THH:mmTHH:mm:ssTHH:mm:ss.sssAll numbers must be base 10. If the MM or DD fields are absent “01” is used as the value. If the HH, mm, or ss fields are absent “00” is used as the value and the value of an absent sss field is “000”. The value of an absent time zone offset is “Z”.Illegal values (out-of-bounds as well as syntax errors) in a format string means that the format string is not a valid instance of this format.NOTE 1 As every day both starts and ends with midnight, the two notations 00:00 and 24:00 are available to distinguish the two midnights that can be associated with one date. This means that the following two notations refer to exactly the same point in time: 1995-02-04T24:00 and 1995-02-05T00:00NOTE 2 There exists no international standard that specifies abbreviations for civil time zones like CET, EST, etc. and sometimes the same abbreviation is even used for two very different time zones. For this reason, ISO 8601 and this format specifies numeric representations of date and time.datestring的格式可以是如下:[date][date]T[time][date]T[time]Z[date]Ttime[0800]其中[date]可以是:YYYYYYYY-MMYYYY-MM-DD而[time]可以是一下:THH:mmTHH:mm:ssTHH:mm:ss.sss可以说[date]是必须有的,其他的都是可以没有,会取默认值,当然各个浏览器内部实现的不一致,所以可能还会出现不一致的问题;API列表Date.UTC()Date.now()Date.parse()Date.prototype.getDate()Date.prototype.getDay()Date.prototype.getFullYear()Date.prototype.getHours()Date.prototype.getMilliseconds()Date.prototype.getMinutes()Date.prototype.getMonth()Date.prototype.getSeconds()Date.prototype.getTime()Date.prototype.getTimezoneOffset()Date.prototype.getUTCDate()Date.prototype.getUTCDay()Date.prototype.getUTCFullYear()Date.prototype.getUTCHours()Date.prototype.getUTCMilliseconds()Date.prototype.getUTCMinutes()Date.prototype.getUTCMonth()Date.prototype.getUTCSeconds()Date.prototype.getYear()Date.prototype.setDate()Date.prototype.setFullYear()Date.prototype.setHours()Date.prototype.setMilliseconds()Date.prototype.setMinutes()Date.prototype.setMonth()Date.prototype.setSeconds()Date.prototype.setTime()Date.prototype.setUTCDate()Date.prototype.setUTCFullYear()Date.prototype.setUTCHours()Date.prototype.setUTCMilliseconds()Date.prototype.setUTCMinutes()Date.prototype.setUTCMonth()Date.prototype.setUTCSeconds()Date.prototype.setYear()Date.prototype.toDateString()Date.prototype.toGMTString()Date.prototype.toISOString()Date.prototype.toJSON()Date.prototype.toLocaleDateString()Date.prototype.toLocaleFormat()Date.prototype.toLocaleString()Date.prototype.toLocaleTimeString()Date.prototype.toSource()Date.prototype.toString()Date.prototype.toTimeString()Date.prototype.toUTCString()Date.prototype.valueOf()Date.prototype[@@toPrimitive]一元加法|加法+new Date()// 15463354481250+new Date()// “0Tue Jan 01 2019 17:53:17 GMT+0800 (中国标准时间)“二元加法获取date的值;加法运算上图中的NOTE 1说,在步骤5和6中的ToPrimitive调用没有提供hint,除了Date对象之外所有ECMAScript对象将缺少hint的情况当做Number处理;js各个类型的toPrimitive而本文需要看的是date@@toPrimitive 方法可以转换一个 Date 对象到一个原始值。Date()Symbol.toPrimitive==>date.toPrimitive;给出的 Date 的原始值。根据传入参数的不同,可以返回 string 或 number 类型。Date 对象的 @@toPrimitive 方法可以返回一个原始值,或是 string,或是number。如果 hint 是 “string” 或 “default”,@@toPrimitive 将会调用 toString。如果 toString 属性不存在,则调用 valueOf。如果 valueOf 也不存在,则抛出一个TypeError。如果 hint 是 “number”,@@toPrimitive 会首先尝试 valueOf,若失败再尝试 toString。当期望一个原始值却收到一个对象时,JavaScript 可以自动的调用 @@toPrimitive 方法来将一个对象转化成原始值,所以你很少会需要自己调用这个方法。date的toPrimitive调用,不传递hint的时候,默认是string;所以:0+new Date()// “0Tue Jan 01 2019 17:53:17 GMT+0800 (中国标准时间)“一元加法获取date的值’+‘号运算符作为一元运算符时,Expression将进行ToNumber操作。可以看到,此时date的toPrimitive调用,传递hint是number,返回毫秒数;参考: JavaScript 加号运算符详解 ...

January 1, 2019 · 5 min · jiezi

如何解析你,Excel的Date呀

简单的背景介绍不久前,我们接了一个自己做前端后端产品的活,从此过上了可怜巴巴敲代码开开心心收获知识的日子呢。那是一个平平无奇的周一下午用户小姐姐在群里说,系统筛选工卡有效期不好使。(系统:不不不,不是我的锅我看了一下数据库,发现,我们原定的有效期格式是这样的整整齐齐。数据库中当时的数据是这样的甚至是这样的看到这种情况,我觉得肯定是输入的时候输的不太对(年轻…于是我决定从Excel下手小姐姐们的操作流程是先用我们的系统导出一份Excel,编辑之后再导入系统的,那只要我把这工卡一列的格式限制为日期,就一定可以统一格式的,嗯。我们项目使用了js-xlsx处理表格的导入导出,下面是导出Excel的伪代码:import * as XLSX from ‘xlsx’;const xlsxMineType = ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’const data = 数据.map((s: any) => ({ID: s.id,工卡有效期: s.card_expired,……}));const sheet = XLSX.utils.json_to_sheet(data);const wb = XLSX.utils.book_new();XLSX.utils.book_append_sheet(wb, sheet, ‘员工信息表’);const wbbuf = XLSX.write(wb, { type: ‘base64’});this.success({ name: “员工信息表.xlsx”, data: wbbuf, type:xlsxMineType });通过 json_to_sheet 可以拿到包含单元格信息的对象{ A2: { t: ’n’, v: 3776 }, B2: { t: ’s’, v: ‘2019-04-01’ }, A3: { t: ’n’, v: 3831 }, B3: { t: ’s’, v: ‘2019-04-01’ }, A1: { t: ’s’, v: ‘ID’ }, B1: { t: ’s’, v: ‘工卡有效期’ }, ‘!ref’: ‘A1:B3’ }对象中以单元格位置作为key,每个单元格的值(v)、类型(t)等等属性作为value。其中单元格的类型支持:b Boolean, n Number, e error, s String, d Date看起来Date类型十分符合上面的要求,就尝试了一下:const sheet = XLSX.utils.json_to_sheet(data);// 筛选出除表头的工卡列Object.keys(sheet).filter(item => /^B/.test(item) && item !== “B1”).forEach(key => { sheet[key].t = “d”;})然鹅,如果工卡有效期本来就为空,这时候导出,打开Excel会报错,并且空的位置会变成NaN翻阅了各种中英文文档、Issue,导出一百多个员工信息表之后,我发现Excel真的很奇妙,或许应该在js上来格式化导入的数据,而不是限制单元格的类型。如果不控制单元格类型的话,那么当管理员输入日期的时候,这个单元格可能是:文本、常规、日期、自定义类型,所以只要保证不管单元格是什么格式,程序都能拿到正确的数据就好了。当管理员使的工卡有效期的单元格类型是文本或者常规的时候,则比较简单,程序可以按预期解析出来一个相应的字符串,用moment解析一下,就可以获得想要的格式的数据了。那么当有效期单元格的类型是日期和自定义的时候,我们拿到的数据是像下图一样这也就是之前数据库中奇怪的数字的由来,这个数字的意义,其实是当前日期距离1900年1月0日的天数。还需要注意的是,Excel中有个bug:它以为1900年是闰年,所以我们拿到的天数都会多了一天,因为转换之前还需要先进行减一操作…item.工卡有效期 = new Date(1900, 0, expried - 1)这样之后就可以拿到正确的日期啦。咕叽。 ...

December 6, 2018 · 1 min · jiezi