js time object related functions

1. Time format conversion

1. Create a global js method library

/**
 * 时间格式化函数, 按照指定格式化字符串格式化传入时间
 * 
 * @param {Date} time 需要格式化的时间
 * @param {String} fmStr 定义时间的格式
 * 		yyyy: 代表四位数年份
 * 		  yy: 代表两位数年份 
 * 		  mm: 代表月份(小于10时补0)
 * 		  dd: 代表日期(小于10时补0)
 * 		  hh: 代表小时(小于10时补0)
 * 		  hh: 代表小时(小于10时补0)
 * 		  MM: 代表分钟(小于10时补0)
 * 		  ss: 代表秒数(小于10时补0)
 * 		 SSS: 代表毫秒数
 * 		   w: 代表周几(数字) 
 * 		   W: 代表周几(中文) 
 * 		  ww: 代表周几(英文) 
 * @returns {String} 返回格式化的时间
 */
 const timeFormat=function timeFormat(time, fmStr) {
    
    
	const weekCN = '一二三四五六日';
	const weekEN = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

	let year = time.getFullYear();
	let month = time.getMonth() + 1;
	let day = time.getDate();
	let hours = time.getHours();
	let minutes = time.getMinutes();
	let seconds = time.getSeconds();
	let milliSeconds = time.getMilliseconds();
	let week = time.getDay();

	month = month >= 10 ? month : ('0' + month);
	day = day >= 10 ? day : ('0' + day);
	hours = hours >= 10 ? hours : ('0' + hours);
	minutes = minutes >= 10 ? minutes : ('0' + minutes);
	seconds = seconds >= 10 ? seconds : ('0' + seconds);

	if (fmStr.indexOf('yyyy') !== -1) {
    
    
		fmStr = fmStr.replace('yyyy', year);
	} else {
    
    
		fmStr = fmStr.replace('yy', (year + '').slice(2));
	}
	fmStr = fmStr.replace('mm', month);
	fmStr = fmStr.replace('dd', day);
	fmStr = fmStr.replace('hh', hours);
	fmStr = fmStr.replace('MM', minutes);
	fmStr = fmStr.replace('ss', seconds);
	fmStr = fmStr.replace('SSS', milliSeconds);
	fmStr = fmStr.replace('W', weekCN[week - 1]);
	fmStr = fmStr.replace('ww', weekEN[week - 1]);
	fmStr = fmStr.replace('w', week);

	return fmStr;
}
  export default {
    
    
    timeFormat
  }
  
  

2. Import in main.js

import globalFn from './util/globalFn'
Vue.prototype.$format=globalFn.timeFormat

3. Use in the page

      var time = new Date();     //Tue Feb 23 2021 21:42:16 GMT+0800 (中国标准时间)
      var fm = this.$format(time,'yyyy-mm-dd hh:MM');   //2021-02-23 21:42

Second, get the first second of the month, and get the last second of the month

// 本月第一秒
    getCurrentMonthFirst () {
    
    
      var date = new Date()
      date.setDate(1)   //设置为当月的第一天
      return new Date(new Date(date).toLocaleDateString())
    },
    // 本月最后一秒
    getCurrentMonthLast () {
    
    
      var date = new Date()
      var currentMonth = date.getMonth()
      var nextMonth = ++currentMonth
      var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)   //获取下一月的1号
      return new Date(nextMonthFirstDay - 1000)
    },

Guess you like

Origin blog.csdn.net/weixin_42349568/article/details/114003843