aurora blog
  • JS

    • 基本汇总
    • Date时间
    • Array方法
    • String 方法
    • Object方法
    • RegExp正则
    • Es6 新特性等
    • Event-Loop
    • Http and Https
    • for in和for of区别
    • Web Worker
    • Promise && async
    • 堆内存 & 栈内存
    • JS设计模式探索
    • npm & yarn
    • Fetch和Axios的区别
    • 学习及面试问题整理
    • URL 输入到页面加载过程
    • 跨域&nginx本地项目代理
  • FE框架

    • react

      • 基本用法
      • react生命周期
      • hooks 16.8版本后
      • Route原理
      • Redux源码解析
      • React16 Fiber
      • React-VirtualDOM
      • React事件委托机制
    • vue

      • vue 基本用法
      • vue 生命周期
      • VirtualDOM
      • vue 事件委托
      • vue 架构
      • vue 状态管理
      • 问题等
    • React-Native

      • React-Native 基本用法
    • 微前端

      • 遇到的问题
    • 鸿蒙 harmony

      • harmony 基础
  • 构建工具

    • webpack

      • Webpack配置详解
      • Webpack的使用
      • Babel-polyfill
      • webpack面试题
    • vite

      • vite基本配置
  • Typescript

    • Typescript详解
  • Servers

    • Nodejs
    • Nginx
  • Git命令

    • git常用规范
  • 数据库

    • mongodb
    • mongodb
  • Other

    • Jenkins自动化部署

Date时间

  • 时间戳转标准时间
  • 倒计时时间格式化
  • 距离现在多久
// 时间戳
Date.now()
new Date().getTime();
new Date().valueOf()

// 设置时间
new Date("2019-06-16 07:55:55"); // Sun Jun 16 2019 07:55:55 GMT+0800 (中国标准时间)

// 时间格式化 
date2 = new Date().toISOString().slice(0,10);   // 2019-06-16

typeof date2 // 'object'

时间戳转标准时间

// 方法一
/*
** 时间戳转换成指定格式日期
** eg.
** dateFormat(1626750568413, 'Y年m月d日 H时i分')
** → "2021年07月20日 11时09分"
*/
const dateFormat = (timestamp, formats) => {
    // formats格式包括
    // 1. Y-m-d
    // 2. Y-m-d H:i:s
    // 3. Y年m月d日
    // 4. Y年m月d日 H时i分s秒
    formats = formats || 'Y-m-d';

    var zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };

    let myDate = timestamp? new Date(timestamp): new Date();

    let year = myDate.getFullYear();
    let month = zero(myDate.getMonth() + 1);
    let day = zero(myDate.getDate());

    let hour = zero(myDate.getHours());
    let minite = zero(myDate.getMinutes());
    let second = zero(myDate.getSeconds());

    return formats.replace(/Y|m|d|H|i|s/ig, function (matches) {
        return ({
            Y: year,
            m: month,
            d: day,
            H: hour,
            i: minite,
            s: second
        })[matches];
    });
};

// 方法二
const dateFormat = (timeStamp) => {
    let date = new Date(timeStamp);
    return date.getFullYear() + "年"
        + (date.getMonth() + 1).toString().padStart(2, 0) + "月"
        + (date.getDate()).toString().padStart(2, 0) + "日 "
        + (date.getHours()).toString().padStart(2, 0) + ":"
        + (date.getMinutes().toString().padStart(2, 0));
}

倒计时时间格式化

function format_time(timeStamp) {
    let day = Math.floor(timeStamp / (24 * 3600 * 1000));
    let leave1 = timeStamp % (24 * 3600 * 1000);
    let hours = Math.floor(leave1 / (3600 * 1000));
    let leave2 = leave1 % (3600 * 1000);
    let minutes = Math.floor(leave2 / (60 * 1000));
    let leave3 = leave2 % (60 * 1000);
    let seconds = Math.floor(leave3 / 1000);
    if (day) return day + "天" + hours + "小时" + minutes + "分";
    if (hours) return hours + "小时" + minutes + "分" + seconds + "秒";
    if (minutes) return minutes + "分" + seconds + "秒";
    if (seconds) return seconds + "秒";
    return "时间到!";
}

距离现在多久

const SECOND = 1000
const MINUTE = SECOND * 60
const HOUR = MINUTE * 60
const DAY = HOUR * 24

function formatToInterval(timestamp) {
    let now = Date.now()
    let value = now - timestamp
    value = value < 1 ? 1 : value

    if (value < MINUTE) {
      return Math.floor(value / SECOND) + '秒前'
    }
    if (value < HOUR) {
      return Math.floor(value / MINUTE) + '分钟前'
    }
    if (value < DAY) {
      return Math.floor(value / HOUR) + '小时前'
    }
    return format(timestamp, 'MM月DD日')
}
最近更新:: 2021/8/16 22:13
Contributors: sountstars
Prev
基本汇总
Next
Array方法