dayjs().format('a') // am
dayjs().format('A') // AM
// @ The offset from UTC, ±HHmm
dayjs().format('ZZ') // +0100
// @ The millisecond, 3-digits
dayjs().format('SSS') // 912
dayjs().format('h:mm A ') // 8:28 AM
dayjs().format('h:mm:ss A') // 8:30:51 AM
dayjs().format('MM/DD/YYYY') // 08/19/2022
dayjs().format('M/D/YYYY') // 8/19/2022
dayjs().format('ddd, hA')
// Output: "Fri, 8AM"
dayjs().format('MMM D, YYYY')
// Aug 19, 2022
我们还可以看到一些中间日期格式:
dayjs().format('ddd, MMM D, YYYY h:mm A ');
// @ Output: Fri, Aug 19, 2022 8:23 AM
dayjs().format('MMM D, YYYY h:mm A');
// Aug 19, 2022 8:24 AM
dayjs().format('dddd, MMMM D YYYY, h:mm:ss A')
// Output: "Friday, August 19 2022, 8:15:51 AM"
dayjs().format('dddd, MMMM Do YYYY, h:mm:ss A')
// output => "Friday, August 19o 2022, 8:15:51 AM"
相对时间插件
在继续其他示例之前,让我们讨论一下 RelativeTime 插件。使用相对时间插件javascript获取日期,日期和时间数字可以转换为相对句子,例如“5 小时前”。使用
CD 安装浏览器:我们必须使用 相对时间CDN,然后使用日.js对其进行设置,以便相对时间插件正常工作。
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.5/dayjs.min.js"
integrity="sha512-Ot7ArUEhJDU0cwoBNNnWe487kjL5wAOsIYig8llY/l0P2TUFwgsAHVmrZMHsT8NGo+HwkjTJsNErS6QqIkBxDw=="
crossorigin="anonymous" referrerpolicy="no-referrer">
其次,从 cdnjs.com 获得的CDN。
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.5/plugin/relativeTime.min.js">
将相对时间插件设置为天.js:
dayjs.extend(window.dayjs_plugin_relativeTime)
由 Node .js 和 ES6 导出的相对时间插件
使用 npmidayjs 时,RelativeTime 插件位于 Day.js 包中。您只需要 Day 中的 RelativeTime 路径即可使用它。
const dayjs = require('dayjs')
var relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)
ES6导入
import Dayjs from "dayjs";
import relativeTIme from "dayjs/plugin/relativeTime";
Dayjs.extend(relativeTIme);
从 X 获取时间
提供表示相对于 X 的时间字符串。相对时间插件将允许我们这样做。
仍然确保使用 dayjs.extend 配置相关的插件。您可以阅读有关 Day.js 插件的更多信息。
dayjs.extend(window.dayjs_plugin_relativeTime);
var a = dayjs("2022-01-01");
console.log(dayjs("2020-01-01").from(a))
// Output: 2 years ago
如果传递 true,则可以获取不带后缀的值。
dayjs.extend(window.dayjs_plugin_relativeTime);
var a = dayjs("2022-01-01");
console.log(dayjs("2020-01-01").from(a, true))
// Output: 2 years
立即开始估算时间
这将转换相对于现在的时间字符串。今天需要一个相对时间插件。
dayjs.extend(window.dayjs_plugin_relativeTime);
console.log(dayjs('2000-01-01').fromNow())
来自未来:
dayjs.extend(window.dayjs_plugin_relativeTime);
console.log(dayjs('2050-01-01').fromNow())
// Output: in 27 years
无后缀:you] 可以提供具有真实布尔值的返回日期字符串。
dayjs.extend(window.dayjs_plugin_relativeTime);
dayjs('2000-01-01').fromNow(true) // Output: 23 years
获取当前时间
这将返回一个字符串,表示到现在为止的相对时间。请记住,这取决于相对时间插件。
dayjs.extend(window.dayjs_plugin_relativeTime);
dayjs('1980-01-01').toNow() // Output: in 43 years
缺少后缀
dayjs('1980-01-01').toNow(true) // Output: 43 years
如何为日期生成 Unix 时间戳
这将给出 Day.js 对象的 Unix 时间戳,即自 Unix 时代以来的秒数。Unix 时间戳对象是 Day .js 中的外部对象,因此您无需调用插件即可使用它们。
无微秒:
dayjs('2019-01-25').unix() // Output: 1548370800
以微秒为单位:
dayjs('2019-01-25').valueOf() // Output: 1548370800000
根据 Day .js 文档,仍然建议使用 Unix 时间戳。
估计一个月中的天数
获取当月没有插件的天数:
dayjs('2020-02-04').daysInMonth() // Output: 29
将日期作为对象返回
为了以对象格式返回日期javascript获取日期,您应该将 toObject 插件与 CDN 一起使用.js或者在节点或 ES6 导出中请求它。
加元:
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.5/plugin/toObject.min.js"
integrity="sha512-qWOc7v2jfO5Zg34fVOIfnpvDopsqDBilo8Onabl/MHIr5idHpg73tVRUGDMVOQs2dUEsmayiagk75Ihjn6yanA=="
crossorigin="anonymous" referrerpolicy="no-referrer">
节点.js
var dayjs = require('dayjs')
var toObject = require('dayjs/plugin/toObject')
dayjs.extend(toObject)
Augment CDN:: with toObject()。
dayjs.extend(window.dayjs_plugin_toObject);
dayjs('2020-01-25').toObject()
输出:
{date: 25, hours: 0, milliseconds: 0, minutes: 0, months: 0, seconds: 0, years: 2020}
返回日期作为形式参数
为了以字段格式返回日期,应使用带有 CDN 的 ToArray 插件,或在节点或 ES6 导出中请求.js或 ES6 导出。
加元:
节点.js
var toArray = require('dayjs/plugin/toArray')
dayjs.extend(toArray)
dayjs.extend(window.dayjs_plugin_toArray);
dayjs('2022-08-04').toArray() // Output: [2022, 7, 4, 0, 0, 0, 0]
以 JSON 格式获取时间和日期
将其序列化为 ISO8601 字符串格式,无需插件:
dayjs('2019-06-25').toJSON() // Output: 2019-06-24T23:00:00.000Z
dayjs('1996-01-11').toJSON() // Output: 1996-01-10T23:00:00.000Z
dayjs('2025-05-10').toJSON() // Output: 2025-05-09T23:00:00.000Z
以字符串形式提供日期和时间
返回一个字符串,其中包含不带插件的日期表示形式:
dayjs('2025-03-20').toString() // Output: Wed, 19 Mar 2025 23:00:00 GMT
dayjs('2010-08-08').toString() // Output: Sat, 07 Aug 2010 23:00:00 GMT
dayjs('01-2005-25').toString() // @ Error output: Invalid Date
解析日期
Day.js 对象是不可变的,这意味着更改它的所有 API 操作也会形成一个新的对象实例。
迄今为止的字符串:
检测以下代码以分析字符串并以日期格式返回它:
dayjs('2020-08-04T15:00:00.000Z')
现有的本机 JavaScriptDate 对象可用于创建 Day .js 对象:
let d = new Date(2021, 02, 11);
let day = dayjs(); // The date returned by the first formatted date is copied in this line
立即使用 Parse
:请参阅下面的代码以使用 Parse 返回当前日期
new Date(2021, 02, 11);
// Alternative
dayjs(new Date());
验证
若要检测日期和时间是否有效,请使用 Day.js 中的 .isValid() 方法。此方法会产生布尔结果:
dayjs('1996-05-01').isValid(); // Output: true
dayjs('dummy text').isValid(); // Output: false
dayjs('2005-06-09').isValid(); // Output: true
时区
Day.js 为遵守相同标准时间的区域提供时区兼容性。使用天.js时区需要插件。要在 Day .js 中使用时区,我们需要同时安装时区和 UTC 插件:
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone') // dependent on utc plugin
dayjs.extend(utc)
dayjs.extend(timezone)
用于 UTC 插件的 CDN
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.5/plugin/utc.min.js"
integrity="sha512-z84O912dDT9nKqvpBnl1tri5IN0j/OEgMzLN1GlkpKLMscs5ZHVu+G2CYtA6dkS0YnOGi3cODt3BOPnYc8Agjg=="
crossorigin="anonymous" referrerpolicy="no-referrer">
时区插件的 CDN
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.5/plugin/timezone.min.js"
integrity="sha512-fG1tT/Wn/ZOyH6/Djm8HQBuqvztPQdK/vBgNFLx6DQVt3yYYDPN3bXnGZT4z4kAnURzGQwAnM3CspmhLJAD/5Q=="
crossorigin="anonymous" referrerpolicy="no-referrer">
天.js扩展时区和UTC插件:
dayjs.extend(window.dayjs_plugin_utc)
dayjs.extend(window.dayjs_plugin_timezone);
例:
dayjs.tz("2020-06-01 12:00", "America/Toronto")
控制台上的输出:
let time = {
$D: 18,
$H: 11,
$L: "en",
$M: 10,
$W: 1,
$d: "Mon Nov 18 2013 11:55:00 GMT+0100 (West Africa Standard Time) {}",
$m: 55,
$ms: 0,
$offset: -300,
$s: 0,
$x: {
$localOffset: -60,
$timezone: "America/Toronto"
}
}
您还可以使用天.js中包含的 .toString() 将时区作为普通字符串返回。
dayjs.tz("2013-11-18 11:55", "America/Toronto").toString()
// Output: Mon, 18 Nov 2013 16:55:00 GMT
区域分辨率
如果要以时区格式解析日期,请使用名为 CurrentParseFormate 的插件来帮助您解析时区:
节点.js
var customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(customParseFormat)
dayjs('05/02/69 1:02:03 PM -05:00', 'MM/DD/YY H:mm:ss A Z')
// Returns an instance containing '1969-05-02T18:02:03.000Z'
CDN
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.5/plugin/customParseFormat.min.js" integrity="sha512-FM59hRKwY7JfAluyciYEi3QahhG/wPBo6Yjv6SaPsh061nFDVSukJlpN+4Ow5zgNyuDKkP3deru35PHOEncwsw==" crossorigin="anonymous" referrerpolicy="no-referrer">
dayjs.extend(window.dayjs_plugin_customParseFormat);
dayjs.tz("12-25-1995", "MM-DD-YYYY", "America/Toronto").toString()
//Mon, 25 Dec 1995 05:00:00 GMT
转换为时区
更新偏斜量,切换时区,然后返回到对象实例.js日期。当第二个参数传递 true 值时,仅修改时区(和偏斜量),而本地时间保持不变。
dayjs.extend(window.dayjs_plugin_utc)
dayjs.extend(window.dayjs_plugin_timezone);
我害怕用户时区
dayjs.extend(window.dayjs_plugin_utc)
dayjs.extend(window.dayjs_plugin_timezone);
dayjs.tz.guess() //Asia/Calcutta
默认时区设置
将首选时区设置为默认时区,而不是本地时区。在某些 dayjs 对象中,您仍然可以自定义不同的时区
节点.js
var utc = require('dayjs/plugin/utc')
var timezone = require('dayjs/plugin/timezone') // dependent on utc plugin
dayjs.extend(utc)
dayjs.extend(timezone)
CDN
dayjs.extend(window.dayjs_plugin_utc)
dayjs.extend(window.dayjs_plugin_timezone);
dayjs.tz.setDefault("America/New_York") // Setting the default time
dayjs.tz("2019-08-05 11:00")
总结
我们首先了解了 Day.js 以及它用于设置较低的时间和日期集的有用性。第 .js 天只是简单地替换了 Moment .js。时刻.js不是必需的,但第 .js 天提供了所有日期槽、解析、插件和本地化要求。因此,您可以使用 dayjs.org 在官方日.js网站上选择和查找更多信息。
源语言:
好
了,明天的内容分享就到这里了,想好朋友记得分享点赞!
PS:工作日上午8:30,CSDN公司正在紧急招聘,继续分享程序员学习和面试相关的干货。
点分享 点收藏 点点赞 点在看 ,具有相同名称的通用)。
关于js中同名的情况(同名变量,同名函数)
,具有相同名称的通用)1. 当变量和函数具有相同的名称时,谁有效
console.log(a); //function a() {} var a; function a() {} console.log(a); //function a() {}
console.log(a); //function a() {} var a = 1; function a() {} console.log(a); //1
个人理解:在以上两种情况下,我发现变量A和函数A同名,区别在于变量A不是参数的形状,我发现复制情况不同。 var 和 funciotn 声明它们将被提出,函数将首先被提出,注意函数表达式不会被提出。
JavaScript在代码执行之前被预编译,创建一个GO(GlobalObject)对象。
去{
a:functiona(){},
在上述情况 1 中,当遇到 vara 时,因变量 a 已经存在,并且被直接忽略。所以副本都是功能。
去{
a:functiona(){}1,
在情况 2 中,当 JS 引擎遇到 a=1 时,它替换了存储在原始变量 a 地址中的指针,参数为 1。
2. 当全局变量和函数中的变量具有相同的名称时,谁有效
var a = 1; function fn() { console.log(a);//1 a; } fn();
var a = 1; function fn() { console.log(a);//undefined var a; } fn();
个人理解:函数执行时会生成函数作用域,作用域也会在预编译时查找 var 和函数声明,如果有的话进行改进。上述情况的区别在于函数中的变量 a 是否有 var 声明:没有 var 声明,是不会改进的,所以在执行到控制台时,如果发现函数范围没有变量 a,就会向下搜索找到变量 a,它的值是 1;如果有 var,就会引发,所以在执行到控制台时,发现函数的作用域是变量 ajavascript的全局函数,值未定义。
3. 当局部变量、函数和函数赋值具有相同名称时,谁有效
function fn(a) { console.log(a); //1 var a = 2; console.log(a); //2 } fn(1);
function fn(a) { console.log(a); //function a() {} function a() {} console.log(a); //function a() {} } fn(1);
调用 fn 函数时,将创建函数执行上下文,然后参数对象根据参数
的参数填充,即:数组 vara=arguments[0],然后根据函数中的函数声明增加函数名 ajavascript的全局函数,这时你会发现函数名和数组名冲突,因为形状参与了参数的数据共享状态。因此,收到后引发的函数名 a 将直接覆盖上面定义的数组名 a 指向的参数