首页 > 其他分享 >js Date汇总

js Date汇总

时间:2023-04-07 16:24:30浏览次数:30  
标签:console log 汇总 js 2023 Date new toLocaleString

// ----------  JavaScript Date  ----------

// Date方法:常用分三大类
/*
  get*:
    getFullYear getMonth getDate getHours getMinutes getSeconds
    getDay
    getTime
*/
/*
  set*
    setFullYear setMonth setDate setHours setMinutes setSeconds
    setTime
*/
/*
  to*
    toDateString toTimeString toString
    toLocaleDateString toLocaleTimeString toLocaleString
    toJSON toGMTString toISOString
*/

// getFullYear
// 获取四位数的年份
console.log( new Date().toLocaleString()); // 2023/3/8 17:26:13
console.log( new Date().getFullYear() ) // 2023

// getMonth
// 获取日期的的月份(0-11) 一月为 0, 二月为 1, 以此类推。
console.log( new Date().toLocaleString()); // 2023/3/8 17:25:40
console.log( new Date().getMonth() ) // 2

// getDate
// 获取一月中的某一天(1-31)
console.log( new Date().toLocaleString()); // 2023/3/8 17:24:54
console.log( new Date().getDate() ) // 8

// getHours
// 获取一天中的小时(0-23)
console.log( new Date().toLocaleString()); // 2023/3/8 17:24:38
console.log( new Date().getHours() ) // 17

// getMinutes
// 获取日期的的分钟数(0-59)
console.log( new Date().toLocaleString()); // 2023/3/8 17:24:12
console.log( new Date().getMinutes() ) // 24

// getSeconds
// 获取日期的秒数(0-59)
console.log( new Date().toLocaleString()); // 2023/3/8 17:23:55
console.log( new Date().getSeconds() ) // 55

// getMilliseconds
// 获取日期的毫秒数(0-999)
console.log( new Date().toLocaleString()); // 2023/3/8 17:23:27
console.log( new Date().getMilliseconds() ) // 130

// getDay
// 获取一周中的某一天 (0 ~ 6)  星期天为 0, 星期一为 1, 以此类推
console.log( new Date().toLocaleString()); // 2023/3/8 17:20:02
console.log( new Date().getDay() ) // 3

// getTime
// 获取1970-1-1至今的毫秒数
// Date.getTime()
const d_getTime = new Date(2000, 5, 1) // 月份需加1,实际为 2000/6/1日
console.log(d_getTime.toLocaleString()) // 2000/6/1 00:00:00
console.log(d_getTime.getTime()) // 959788800000

const d_getTime0 = new Date(1970, 1, 1) // 月份需加1,实际为 1970/2/1日
console.log(d_getTime0.toLocaleString()) // 1970/2/1 00:00:00
console.log(d_getTime0.getTime()) // 2649600000

const d_getTime01 = new Date(1970, 0, 1, 8, 0, 0) // 月份需加1,实际为 1970/1/1日
console.log(d_getTime01.toLocaleString()) // 1970/1/1 08:00:00
console.log(d_getTime01.getTime()) // 0  ( ****** ******)
console.log(new Date(0).toLocaleString()); // 1970/1/1 08:00:00 (**********)

const d_getTime1 = new Date(1970, 0, 1) // 月份需加1,实际为 1970/1/1日
console.log(d_getTime1.toLocaleString()) // 1970/1/1 00:00:00
console.log(d_getTime1.getTime()) // -28800000  ( ****** )

const d_getTime2 = new Date(1960, 1, 1) // 月份需加1,实际为 1960/2/1日
console.log(d_getTime2.toLocaleString()) // 1960/2/1 00:00:00
console.log(d_getTime2.getTime()) // -312969600000

console.log(new Date(1970, 0, 1, 0, 0, 0).getTime());


// setFullYear
// 设置Date中年份(四位数字)
console.log( new Date().toLocaleString()); // 2023/3/8 17:16:39
console.log( new Date(new Date().setFullYear(2022)).toLocaleString() ); // 2022/3/8 17:16:39

// setMonth
// 设置Date中月份 (0 ~ 11)
console.log( new Date().toLocaleString()); // 2023/3/8 17:15:39
console.log( new Date(new Date().setMonth(11)).toLocaleString() ); // 2023/12/8 17:15:39

// setDate
// 设置Date中月的某一天(1-31)
console.log( new Date().toLocaleString()); // 2023/3/8 17:14:57
console.log( new Date(new Date().setDate(30)).toLocaleString() ); // 2023/3/30 17:14:57

// setHours
// 设置Date中的小时(0-23)
console.log( new Date().toLocaleString()); // 2023/3/8 17:14:08
console.log( new Date(new Date().setHours(20)).toLocaleString() ); // 2023/3/8 20:14:08

// setMinutes
// 设置Date中的分钟 (0 ~ 59)
console.log( new Date().toLocaleString()); // 2023/3/8 17:13:22
console.log( new Date(new Date().setMinutes(30)).toLocaleString() ); // 2023/3/8 17:30:22

// setSeconds
// 设置Date中的分钟 (0 ~ 59)
console.log( new Date().toLocaleString()); // 2023/3/8 17:12:44
console.log( new Date(new Date().setSeconds(30)).toLocaleString() ); // 2023/3/8 17:12:30


// toDateString
console.log( new Date().toLocaleString()); // 2023/3/8 16:30:21
console.log( new Date().toDateString()); // Wed Mar 08 2023

// toTimeString
console.log( new Date().toLocaleString()); // 2023/3/8 16:30:48
console.log( new Date().toTimeString()); // 16:30:48 GMT+0800 (中国标准时间)

// toString
console.log( new Date().toString() ) // 'Wed Mar 08 2023 16:21:37 GMT+0800 (中国标准时间)'


// toLocaleDateString
console.log( new Date().toLocaleDateString() ); // 2023/3/8

// toLocaleTimeString
console.log( new Date().toLocaleTimeString() ); // 16:23:05

// toLocalString
console.log( new Date().toLocaleString() ); // 2023/3/8 16:22:26


// toGMTString
console.log( new Date().toLocaleString()); // 2023/3/8 16:29:35
console.log( new Date().toGMTString() ); // Wed, 08 Mar 2023 08:29:35 GMT

// toISOString
// 注意: 转为时间为 UTC 时间,即与北京时间相差8小时
console.log( new Date().toLocaleString()); // 2023/3/8 16:28:18
console.log( new Date().toISOString() ); // 2023-03-08T08:28:18.183Z

// toJSON
// 注意: 转为时间为 UTC 时间,即与北京时间相差8小时
console.log( new Date().toLocaleString() ); // 2023/3/8 16:26:47
console.log( new Date().toJSON() ); // 2023-03-08T08:26:47.155Z



// 其他说明
/*
  1、UTC 时间与上类似,不单独说明

  2、将 / 转为 - , 参考:
    new Date('2023-03-08T20:20:51.214').toLocaleString()
    '2023/3/8 20:20:51'
    new Date('2023-03-08T20:20:51.214').toLocaleString().replace(/\//g, '-')
    '2023-3-8 20:20:51'
*/

 

标签:console,log,汇总,js,2023,Date,new,toLocaleString
From: https://www.cnblogs.com/-roc/p/17296557.html

相关文章

  • js String汇总
    //----------JavaScriptString----------//charAtcharCodeAt//concatrepeatsplit//slicesubstring//replacereplaceAll//toLowerCasetoUpperCase//startsWithendsWith//indexOflastIndexOfincludes//matchsearch//charAt()//返回在指定位......
  • js RegExp汇总
    //----------JavaScriptRegExp----------/*语法:constpatt=newRegExp(pattern,modifiers)或constpatt=/pattern/modifierspattern(模式)描述了表达式的模式modifiers(修饰符)用于指定全局匹配、区分大小写的匹配和多行匹配*///修饰符-用于执......
  • JS生成随机颜色
    //传统写法functionrandomColor1(){varr=Math.floor(Math.random()*256),g=Math.floor(Math.random()*256),b=Math.floor(Math.random()*256);return`rgb(${r},${g},${b})`;}//取巧functionrand......
  • js数据遍历几种方式
    在JavaScript中,有多种方式可以遍历数据集,下面列出了常用的几种:for循环for循环是一种常见的遍历数据集的方式,可以用于遍历数组、对象等数据类型。例如:constarr=[1,2,3];for(leti=0;i<arr.length;i++){console.log(arr[i]);}constobj={a:1,b:2,......
  • h5 - pc 使用 pdf.js 预览pdf -配合文件流实现 - 遇到的坑总结
    1.pdf.js下载看我这篇随笔【h5-使用pdf.js预览pdf-岑惜-博客园(cnblogs.com)】2.html调用页面的局部代码<body><divstyle="height:100vh;margin:0auto"><iframestyle="height:100%;width:100%;border:none"id="fvic"src="&......
  • Spring Boot返回Json数据及数据封装
    1.1简介在项目开发中,接口与接口之间,前后端之间数据的传输都使用Json格式,在SpringBoot中,接口返回Json格式的数据很简单,在Controller中使用@RestController注解即可返回Json格式的数据,@RestController也是SpringBoot新增的一个注解,我们点进去看一下该注解都包含了哪些东西......
  • 小程序开发 JSON转换的使用
    前言  此篇博客讲解小程序的JSON使用,因为JavaScript与JSON泛用性太大。初学者很容易在JSON的使用上困惑。 字符串转JSON请注意,下面的字符串json是带引号的。jsonTest(){letjsonString="{\"id\":\"1\",\"name\":\"测试JSON\"}";letdata......
  • JS 字符串特殊字符全部替换空
    1、方法constformatStr=(str)=>{constvalue=str.replace(/[`:_~!@#$%^&*()\+=<>?"{}|,\/;'\\[\]·~!@#¥%……&*()——\+={}|《》?:“”【】、;‘’,。、-]/g,'',)returnvalue}2、实例......
  • [记录]php url传参json json_decode 后 null
    从APP端或从其他页面post,get过来的数据一般因为数组形式。因为数组形式不易传输,所以一般都会转json后再发送。本以为发送方json_encode(),接收方json_decode(),就解决的问题,结果发现,json_decode()后是NULL。一般会反应是少了一个参数“true”,但是回去看就是 json_decode($data,tru......
  • js 代码箱
    1<!--得出一个永远不会重复的日期格式的字符串,包括毫秒,用于AJAX输出一个不重复参数,以避免不刷新,function全部写在head节点内-->2<scripttype="text/javascript">3functiongetNowTime(){4vardate=newDate();5this.year=date.getFu......