如何创建日期对象

1.创建日期对象:通过new Date()创建
    new 创建一个新的, 申请内存空间的作用存储日期对象
    Date() 构造函数:用来创建对象的, 函数名首先字母是大写
    now={}

 var now = new Date()
        console.log(now)

2.创建日期对象 Date带参数,参数是字符串时间,把参数转成对象格式

var before = new Date("2022-07-11 15:32:30")
        console.log(before)

3.创建日期字符串

 var s1 = Date()
 console.log(s1)

4.Date是js内置对象
    now() 获取当前时间距离1970年1月1日的ms数 时间戳
        var d1 = Date.now()
        console.log(Date)
        console.log(d1)  //1657527277662

5. 把日期字符串转成时间ms数
        var d2 =  Date.parse("2022-07-11 15:32:30")
        console.log(d2)

6 如何把ms转成日期对象  把d1=1657527277662转成年月日
        var n2 = new Date(d1)
        console.log(n2)
        console.log(n2.getFullYear())   //2022
        console.log(n2.getMonth()+1)

7 把日期对象转成字符串
        console.log(n2.toDateString()) // 只保留年月日字符串
        console.log(n2.toString()) // 转成字符串(重要)
        console.log(n2.toTimeString())// 只保留时间

console.log(now.getMonth()+1) //获取月份之后加1
        console.log(now.getDate())  // 获取日期
        console.log(now.getHours())  //获取小时
        console.log(now.getMinutes()) //获取分钟
        console.log(now.getSeconds()) // 获取秒
        console.log(now.getDay()) // 星期几,星期天是0
        console.log(now.getMilliseconds()) //获取ms数  1s=1000ms

         console.log(now.getFullYear()) //获取年的 2022

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值