html常用代码大全汇总 常用简单的js代码大全

常用简单的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().timeZone22个关于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\\\'}`;Demo
suffixAmPm(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));Demo
diffDays(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());Demo
monthDiff(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();Demo
compare(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\\\'));Demo
formatSeconds(200); // 00:03:20formatSeconds(500); // 00:08:20从日期中提取年、月、日、时、分、秒和毫秒JavaScript 版本
// `date` 是一个 `Date` 对象const extract = (date) => date .toISOString() .split(/[^0-9]/) .slice(0, -1);// `extract` 是一个 [年、月、日、小时、分钟、秒、毫秒] 的数组TypeScript 版本

推荐阅读