常用简单的js代码大全(html常用代码大全汇总),小编带你了解更多信息 。
日常开发中,Date对于我们来说是经常使用到的,平常使用的一些UI框架,如Antd、ElementUI等都会使用像moment.js、day.js的时间工具库去处理它们关于时间的组件,但当我们脱离这些框架,想去解决一个关于时间转换的问题,引入一个时间工具库就显得冗余了,由此这里给大家分享22个关于Date实用的一行Javascript与typescript代码大全,建议收藏阅读!前置知识
Intl 对象是 ECMAScript 国际化 API 的一个命名空间,它提供了精确的字符串对比、数字格式化,和日期时间格式化 。Collator,NumberFormat 和 DateTimeFormat 对象的构造函数是 Intl 对象的属性 。了解更多Intl.DateTimeFormat是根据语言来格式化日期和时间的对象的构造器
语法:
// locales为语言new Intl.DateTimeFormat([locales[, options]])Intl.DateTimeFormat.call(this[, locales[, options]])// 格式化对应语言得日期,date为Date实例,返回日期字符串new Intl.DateTimeFormat(locale).format(date)// 获取时区Intl.DateTimeFormat().resolvedOptions().timeZone
22个关于Date实用的一行Javascript与typescript代码大全为小时添加AM/PMJavaScript 版本
// `h` 是 0 到 23 之间的小时数const suffixAmPm = (h) => `${h % 12 === 0 ? 12 : h % 12}${h < 12 ? \\\'am\\\' : \\\'pm\\\'}`;
TypeScript 版本const suffixAmPm = (h: number): string => `${h % 12 === 0 ? 12 : h % 12}${h < 12 ? \\\'am\\\' : \\\'pm\\\'}`;
DemosuffixAmPm(0); // \\\'12am\\\'suffixAmPm(5); // \\\'5am\\\'suffixAmPm(12); // \\\'12pm\\\'suffixAmPm(15); // \\\'3pm\\\'suffixAmPm(23); // \\\'11pm\\\'
计算两个日期之间的差异天数JavaScript 版本const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
TypeScript 版本const diffDays = (date: Date, otherDate: Date): number => Math.ceil(Math.abs(date.valueOf() - otherDate.valueOf()) / (1000 * 60 * 60 * 24));
DemodiffDays(new Date(\\\'2014-12-19\\\'), new Date(\\\'2020-01-01\\\')); // 1839
计算两个日期之间的月数JavaScript 版本const monthDiff = (startDate, endDate) => Math.max(0, (endDate.getFullYear() - startDate.getFullYear()) * 12 - startDate.getMonth() + endDate.getMonth());
TypeScript 版本const monthDiff = (startDate: Date, endDate: Date): number => Math.max(0, (endDate.getFullYear() - startDate.getFullYear()) * 12 - startDate.getMonth() + endDate.getMonth());
DemomonthDiff(new Date(\\\'2020-01-01\\\'), new Date(\\\'2021-01-01\\\')); // 12
比较两个日期JavaScript 版本// `a` 和 `b` 是 `Date` 实例const compare = (a, b) => a.getTime() > b.getTime();
TypeScript 版本const compare = (a: Date, b: Date): boolean => a.getTime() > b.getTime();
Democompare(new Date(\\\'2020-03-30\\\'), new Date(\\\'2020-01-01\\\')); // true
将日期转换为 YYYY-MM-DD 格式JavaScript 版本// `date` 是对象实例const formatYmd = (date) => date.toISOString().slice(0, 10);
TypeScript 版本const formatYmd = (date: Date): string => date.toISOString().slice(0, 10);
Demo【html常用代码大全汇总 常用简单的js代码大全】
formatYmd(new Date()); // 2020-05-06
将秒转换为 hh:mm:ss 格式JavaScript 版本// `s` 秒数const formatSeconds = (s) => new Date(s * 1000).toISOString().substr(11, 8);// Orconst formatSeconds = (s) => new Date(s * 1000).toUTCString().match(/(\\\\d\\\\d:\\\\d\\\\d:\\\\d\\\\d)/)[0];// Orconst formatSeconds = (s) => [parseInt(s / 60 / 60), parseInt((s / 60) % 60), parseInt(s % 60)].join(\\\':\\\').replace(/\\\\b(\\\\d)\\\\b/g, \\\'0$1\\\');
TypeScript 版本const formatSeconds = (s: number): string => new Date(s * 1000).toISOString().substr(11, 8);// Orconst formatSeconds = (s: number): string => (new Date(s * 1000).toUTCString().match(/(\\\\d\\\\d:\\\\d\\\\d:\\\\d\\\\d)/) as string[])[0];// Orconst formatSeconds = (s: number): string => ( [parseInt(`${s / 3600}`), parseInt(`${(s / 60) % 60}`), parseInt(`${s % 60}`)].join(\\\':\\\').replace(/\\\\b(\\\\d)\\\\b/g, \\\'0$1\\\'));
DemoformatSeconds(200); // 00:03:20formatSeconds(500); // 00:08:20
从日期中提取年、月、日、时、分、秒和毫秒JavaScript 版本// `date` 是一个 `Date` 对象const extract = (date) => date .toISOString() .split(/[^0-9]/) .slice(0, -1);// `extract` 是一个 [年、月、日、小时、分钟、秒、毫秒] 的数组
TypeScript 版本
推荐阅读
- 啪嗒砰3金手指代码,啪嗒砰3快速升级金手指
- 北方萝卜的储存方法 储藏胡萝卜的常用方法
- vivoy3s有没有快充 vivoy3s有没有闪充
- 临沂职业技术学院2021招生简章 临沂职业学院招生代码2022
- 鸡用兽药经典配方 鸡养殖常用的中兽药配方
- tell的用法和固定搭配 常用搭配推荐
- 电脑宽带连接错误代码678怎么办 电脑网络连接错误代码678该怎么解决
- 这样修剪更符合审美「论述常用的修剪手法」
- 用echo输出html语言中a如何加target属性 target属性作用
- 计算机常用数制有哪些 计算机常用数制解释