js Date使用

本文介绍了JavaScript中newDate()函数的不同用法,展示了如何创建不同格式的日期对象,并提供了获取当前时间戳的方法。此外,还详细讲解了在Vue中使用element-ui的el-date组件限制日期选择范围到今天之后的示例。最后,给出了获取上个月第一天和最后一天的函数实现。
摘要由CSDN通过智能技术生成


记录一下js日期的使用

一、new Date()创建

// new Date('MM dd,YYYY hh:mm:ss')
const date1 = new Date('02 03,2022 14:22:33')
// new Date('YYYY/MM/dd')
const date2 = new Date('2022/05/15')
// new Date('YYYY,MM,dd')
const date3 = new Date('2022,05,16')
// new Date('MM,dd,YYYY')
const date4 = new Date('05,19,2022')
// new Date(ms) 时间戳
const date5 = new Date(1652580133948)

获取当前时间戳

// 时间戳表示一天
const one_day = 86400000		// 24 * 60 * 60 * 1000
// 获取当前时间戳
const current1 = new Date().getTime()
const current2 = Date.now()

二、vue中使用

1.element中el-date 组件,只展示今天及以后的日期

<template>
	/**
	*	 value-format: 值的格式;
	*	 format:展示的格式;
	*	 picker-options:选择器可选范围;
	*/
	<el-date-picker
      	v-model="form.startTime"
       :picker-options="beforeDate"
       format="yyyy-MM-dd"
       value-format="timestamp"
       type="date"
       placeholder="选择日期"/>
</template>
<script>
		date(){
			return {
				form:{
					startTime: ''
				},
				beforeDate: {
			        disabledDate(date) {
		          		return date.getTime() <= Date.now() - 24 * 60 * 60 * 1000
		          	}
		        }
			}
		}
</script> 

三、获取上月第一天和最后一天

queryLastMonthTime() {
      const date = new Date()
      let year = date.getFullYear()
      // 当前月
      const current_month = date.getMonth()
      // 当前月第一天
      const current_firstDay = new Date(year, current_month, 1)
      // 上月最后一天
      const last_lastDay = new Date(current_firstDay.getTime() - 1000 * 60 * 60 * 24)
      // console.log(last_lastDay.getMonth(), last_lastDay.getDate())

      let last_month = date.getMonth() - 1
      if (last_month < 0) {
      // 1月重新赋值
        year -= 1
        last_month = 11
      }

      // 上月第一天
      const last_firstDay = new Date(year, last_month, 1)
      // console.log(last_firstDay.getMonth(), last_firstDay.getDate())
      return [last_firstDay.getTime(), last_lastDay.getTime()]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洋哥登陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值