zoukankan      html  css  js  c++  java
  • JS_Date日期时间对象

    1.获取当前时间

    <script>
        // 获取当前时间
        var now = new Date()
        console.log(now)
    </script>

    结果:

     2.自定义时间

    <script>
        // 字符串方式自定义时间
        var xmas = new Date('December 25,1998 13:30:30')
        console.log(xmas)
        // 参数方式自定义时间,月份(0-11)
        var xmas = new Date(1995,11,25)
        console.log(xmas)
        var xmas = new Date(1995,11,25,14,30,0)
        console.log(xmas)
    </script>

    结果:

     3.常用方法

    <script>
        var now = new Date()
        console.log(now)
        // 获取当前时间在当月的第几天
        console.log(now.getDate())
        // 获取当前时间的当前月份
        console.log(now.getMonth())
        // 获取当前时间的当前年份
        console.log(now.getFullYear())
        // 获取星期几(0-6)0为周末 6为周六 1为周一
        console.log(now.getDay())
        // 获取当前时间的小时
        console.log(now.getHours())
        // 获取当前时间的分钟
        console.log(now.getMinutes())
        // 获取当前时间的秒数
        console.log(now.getSeconds())
    </script>

    结果:

     4.日期格式

    <script>
        var now = new Date()
        // 星期几 月 日 年
        console.log(now.toDateString())
        // 时 分 秒 时区
        console.log(now.toTimeString())
        // 2020/4/18
        console.log(now.toLocaleDateString())
        // 上午11:05:45
        console.log(now.toLocaleTimeString())
        // 2020/4/18 上午11:06:56
        console.log(now.toLocaleString())
        // 国际:Sat, 18 Apr 2020 03:08:43 GMT
        console.log(now.toUTCString())
    </script>

    结果:

  • 相关阅读:
    在iview admin中封装axios请求
    git使用
    css选择器
    vue打包空白及字体路径错误问题
    axios 等待同步请求用法及多请求并发
    在Vuex更新,组件内的视图更新问题
    vue中用ajax上传文件
    在vue中使用lang="scss"出现报错解决思路
    HBuilder打包vue项目app后空白,并请求不到数据
    接口里返回的数据不全问题
  • 原文地址:https://www.cnblogs.com/wangdianchao/p/12670809.html
Copyright © 2011-2022 走看看