题目为 : 写一个模块,外部调用这个模块,请求参数是时间戳,模块要求

今天的时间,统一用24小时写作 03:00、15:04

昨天的时间,统一写昨天

昨天之前的时间,但在本周之内的时间,统一用周一、周二、周三这样来写

上周的时间,统一协作15/3/4,依次是年/月/日

注意当月和日是个位数的时候,不需要用0站位变成两位,其他显示null

function formatTime(time) {
const __time = new Date(time * 1000)
function zeroize(num) {
return (String(num).length == 1 ? '0' : '') + num
} function formatTime1() { //转化成时分秒
return `${zeroize(__time.getHours())}:${zeroize(__time.getMinutes())}:${zeroize(__time.getSeconds())}`
} function formatTime2() { //转化成年月日
return `${__time.getFullYear().toString().substr(2,4)}/${__time.getMonth()+1}/${__time.getDate()}`
} function getWeek() {
const _day = new Date(time * 1000).getDay()
switch (_day) {
case 0:
return '日'
case 1:
return '一'
case 2:
return '二'
case 3:
return '三'
case 4:
return '四'
case 5:
return '五'
case 6:
return '六'
}
}
var now = `${new Date().getFullYear()}/${new Date().getMonth()+1}/${new Date().getDate()} 00:00:00`
var _now = new Date(now).getTime()
//时间戳比今天0点小一天就是昨天
if (time * 1000 < _now && time * 1000 > _now - 24 * 60 * 60 * 1000) {
return `昨天${formatTime1()}`
}
//时间戳大于今天0点就是今天
if (time * 1000 > _now) {
return `今天${formatTime1()}`
}
//时间戳小于昨天的0点大于本周一的时间戳是本周
if (time * 1000 < _now - 24 * 60 * 60 * 1000 && time * 1000 > _now - 24 * 60 * 60 * 1000 * (new Date().getDay() - 1)) {
return `周${getWeek()}`
}
//时间戳小于本周一的时间戳大于上周一的时间戳是上周
if (time * 1000 < _now - 24 * 60 * 60 * 1000 * (new Date().getDay() - 1) && time * 1000 > _now - 24 * 60 * 60 * 1000 * new Date().getDay() - 6 * 24 * 60 * 60 * 1000) {
return formatTime2()
}
return 'null'
}

js自定义格式化时间戳的格式的更多相关文章

  1. JS代码格式化时间戳

    一.[24小时制]yyyy-MM-dd HH:mm:ss new Date().toJSON() // 2019-12-13T13:12:32.265Z 通过上面的方法,基本就可以将日期格式化,然后稍 ...

  2. js数字格式化千分位格式

    带小数点的 var a = 8462948.2453; console.log(a.toLocaleString()) //8,462,948.245 不带小数点的 num.toString().re ...

  3. js格式化时间戳

    //js格式化时间戳,转换为时间格式  2017-1-15 4:10:15 function getLocalTime(nS) { var time = new Date(parseInt(nS) * ...

  4. vue.js怎样将时间戳转化为日期格式

    <!-- value 格式为13位unix时间戳 --><!-- 10位unix时间戳可通过value*1000转换为13位格式 --> export function for ...

  5. js时间格式化函数,支持Unix时间戳

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. JS 时间函数 / 格式化时间戳

    处理时间主要使用时间对象 Date , 其提供两个静态方法 Date.now() //获得当前时间戳 Date.parse() //将字符串转化成时间戳 创建对象 new Date(); // 返回当 ...

  7. js格式化时间 js格式化时间戳

    一个js格式化时间和js格式化时间戳的例子. 代码:/** * 时间对象的格式化; */Date.prototype.format = function(format) { /* * eg:forma ...

  8. 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换

    var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...

  9. js 日期格式化函数(可自定义)

    js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str ...

随机推荐

  1. Linux下间隔多少秒 (即以秒为单位) 去执行某条命令或某个shell脚本的操作方法【转】

    在日常运维工作中, 经常会碰到以秒为单位去定时执行某些命令或监控脚本的需求. 说到定时任务就要用到crontab,通常来说,crontab的最小单位是分钟级别,要想实现秒级别的定时任务,就要进行特殊设 ...

  2. Python内置模块之configparse

    一.概述 1.1.处理的文件形式 configparse 主要是用来处理类似于windows的 ini文件,这个文件的特点是有多个节(section),每个节下会存储多个k=v的值 如下配置 [har ...

  3. __libc_csu_init函数的通用gadget

    . ; =============== S U B R O U T I N E ======================================= . . . public __libc_ ...

  4. loj6077

    题解: 网上的做法好像都是容斥 那就先说一下容斥 首先问题等价于求下面这个式子的方案数 $$\sum_{i=1}^{n} ai (0<ai<i) =k$$ 直接$dp$复杂度是$nk$的, ...

  5. spark DataFrame

    DataFrame的推出,让Spark具备了处理大规模结构化数据的能力,不仅比原有的RDD转化方式更加简单易用,而且获得了更高的计算性能.Spark能够轻松实现从MySQL到DataFrame的转化, ...

  6. CodeForces 528D Fuzzy Search 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8782849.html 题目传送门 - CodeForces 528D 题意 给你两个串$A,B(|A|\geq| ...

  7. 转:mongoDB 修改 ulimit

    转自:http://blog.yucanlin.cn/2015/03/23/mongodb-%E4%BF%AE%E6%94%B9-ulimit/ mongoDB 修改 ulimit 一切都源于mong ...

  8. 【软件工程】分布式版本控制系统Git的安装与使用

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远端库地址:https://github.com/Richa ...

  9. python一个命令开启http服务器

    1.例如想共享文件在   E:python文件 打开cmd cd E: cd python文件 #进入要分享的文件夹 2.执行py脚本文件 python -m http.server 3.访问 本机i ...

  10. 关于WQS二分算法以及其一个细节证明

    应用分析 它的作用就是题目给了一个选物品的限制条件,要求刚好选$m$个,让你最大化(最小化)权值, 然后其特点就是当选的物品越多的时候权值越大(越小). 算法分析 我们先不考虑物品限制条件, 假定我们 ...