CodeHelper CodeHelper
首页
JavaScript
PHP
Python
Git速查表 (opens new window)
博客 (opens new window)
首页
JavaScript
PHP
Python
Git速查表 (opens new window)
博客 (opens new window)
  • 开始

    • 使用
  • 函数方法

    • 字符串
    • 数字
    • 数组
    • 日期时间
      • new Date()
      • 格式化时间
    • 缓存
    • 深浅拷贝
    • 正则
    • md5
    • 其它
  • 梳理

    • 循环
  • 高级操作

    • 类型转换技巧
    • 防抖与节流
    • Promise实现
目录

日期时间

# new Date()

let date = new Date(); // Tue Jul 19 2022 15:05:59 GMT+0800 (中国标准时间)
let date2 = new Date(2022, 10, 03, 12, 16, 44); // Thu Nov 03 2022 12:16:44 GMT+0800 (中国标准时间)

// 获取当前年份(2位) 部分浏览器返回的是 当前年份-1900 的值
date.getYear();

// 获取完整的年份(4位)
date.getFullYear(); 

// 获取当前月份(0-11,0代表1月)
date.getMonth(); 

// 获取当前日(1-31)
date.getDate();

// 获取当前星期X(0-6,0代表星期天)
date.getDay();

// 获取当前时间 - 时间戳13位 (从1970.1.1开始的毫秒数)
date.getTime(); 

// 时间戳10位 (秒)
parseInt( date.getTime()/1000 );

// 获取当前小时数(0-23)
date.getHours(); 

// 获取当前分钟数(0-59)
date.getMinutes();

// 获取当前秒数(0-59)
date.getSeconds();

// 获取当前毫秒数(0-999)
date.getMilliseconds(); 

// 获取当前日期
date.toLocaleDateString(); // '2022/7/19'

// 获取当前时间
date.toLocaleTimeString(); // '15:11:28'

// 获取日期与时间
date.toLocaleString();  // '2022/7/19 15:11:28'

date.toLocaleString('chinese', { hour12: true }) // '2022/7/19 下午3:11:28'

# 格式化时间

参考 (opens new window)

timeFormat(timestamp|date, option)

  • 参数:
    • timestamp|date <String> 任何合法的时间格式、秒或毫秒的时间戳
    • option <Object> 可选参数
      • option.format <String> 时间格式。默认为yyyy-mm-dd,年为"yyyy",月为"mm",日为"dd",时为"hh",分为"MM",秒为"ss",格式可以自由搭配,如: yyyy:mm:dd,yyyy-mm-dd,yyyy年mm月dd日,yyyy年mm月dd日 hh时MM分ss秒,yyyy/mm/dd/,MM:ss等组合
      • option.dif <Number> 加减时间(秒)。默认0
// padStart 的 polyfill,因为某些机型或情况,还无法支持es7的padStart,比如电脑版的微信小程序
// 所以这里做一个兼容polyfill的兼容处理
if (!String.prototype.padStart) {
	// 为了方便表示这里 fillString 用了ES6 的默认参数,不影响理解
	String.prototype.padStart = function(maxLength, fillString = ' ') {
		if (Object.prototype.toString.call(fillString) !== "[object String]") throw new TypeError(
			'fillString must be String')
		let str = this
		// 返回 String(str) 这里是为了使返回的值是字符串字面量,在控制台中更符合直觉
		if (str.length >= maxLength) return String(str)

		let fillLength = maxLength - str.length,
			times = Math.ceil(fillLength / fillString.length)
		while (times >>= 1) {
			fillString += fillString
			if (times === 1) {
				fillString += fillString
			}
		}
		return fillString.slice(0, fillLength) + str;
	}
}

// 其他更多是格式化有如下:
// yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合
function timeFormat(dateTime = null, option = {}) {
  let _option = {fmt: 'yyyy-mm-dd hh:MM:ss', dif: 0}
  Object.assign(_option, option)

	// 如果为null,则格式化当前时间
	if (!dateTime) dateTime = Number(new Date());
	// 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
	if (dateTime.toString().length == 10) dateTime *= 1000;
	let date = new Date(dateTime);
  
  if (_option.dif !== 0) {
    let tmp_dateTime = Number(date);
    tmp_dateTime = tmp_dateTime + (_option.dif * 1000)
    date = new Date(tmp_dateTime);
  }

	let ret;
	let opt = {
		"y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"h+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"s+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加,必须转化成字符串
	};
  let fmt = _option.fmt
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}

export default timeFormat

使用

timeFormat(null)  // '2022-07-21 01:10:02'
timeFormat(null, {fmt: 'yyyy/mm/dd hh时MM分ss秒'}) // '2022/07/21 01时11分44秒'

// 当前时间加两天
timeFormat(null, {dif: 60*60*24*2}) // '2022-07-23 01:11:02'
// 当前时间减30天
timeFormat(null, {fmt: 'yyyy-mm-dd', dif: -(60*60*24*30)}) // '2022-06-21'
上次更新: 2022/08/23, 18:58:25
数组
缓存

← 数组 缓存→

Theme by Vdoing | Copyright © 2022-2023 CodeHelper
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式