zoukankan      html  css  js  c++  java
  • 常用的Date对象和Math对象方法

    Date对象方法:

    当前用户本地时间

    let time = new Date();

    获取整数年

    console.log(time.getFullYear());

    获取当前月(月份要加1)

    console.log(time.getMonth()+1);

    获取当前周【0-6】星期日是0

    console.log(time.getDay());

    获取时间日

    console.log(time.getDate());

    获取小时

    console.log(time.getHours());

    获取分钟

    console.log(time.getMinutes());

    获取时间秒

    console.log(time.getSeconds());

    获取毫秒数

    console.log(time.getMilliseconds());

    获取当前时间距离1970年-1-1日上午八点的毫秒差(常用于时间戳)

    console.log(time.getTime());

    获取时间戳的几种方式

    time = Date.parse(new Date())
    time = (new Date()).valueOf();
    time = new Date().getTime();

    这里顺便说下倒计时的原理以及公式

    原理:

    未来时间-现在时间= 剩余时间

    公式:

    let d = Math.floor(t/86400);
    t % =86400;
    let h = Math.floor(t/3600);
    t % = 3600;
    let m = Math.floor(t/60);
    t % = 60;

    Math对象常用方法:

    返回一组数的最大值

    Math.max()

    返回一组数的最小值

    Math.min()

    向下取整

    Math.floor()

    向上取整

    Math.ceil()

    四舍五入,包括整数

    Math.round()

    返回0-1之间的随机数

    Math.random

    返回数字的绝对值

    Math.abs()

    返回数字的平方根

    Math.sqrt()
  • 相关阅读:
    典型用户和场景总结
    排球的详细记分规则
    js 实现全选
    博客搬家啦!
    leetcode #980 不同路径||| (java)
    ADV-299 宰羊 (java,过了30%)
    ADV-292 计算行列式 java
    ADV-302 秘密行动 java
    ADV-297 快速排序 java
    ADV-298 和谐宿舍2 动态规划
  • 原文地址:https://www.cnblogs.com/theblogs/p/10125479.html
Copyright © 2011-2022 走看看