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

今天的时间,统一用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. js设置document.domain实现跨域

    document.domain 只能实现跨子域的问题 如:xxx.com/a.html 和aaa.xxx.com/b.html 或:bbb,xxx.com/c.html 和ccc.xxx.com/d. ...

  2. Windows7下PHP5.6.19+Apache2.4.18+MySql5.7环境配置

    此安装参考了网上各方资料,最终整理的内容为本次安装涉及的部分. 一.准备安装材料: 1.从http://windows.php.net/download/ 下载5.6.19 线程安全版(使用apach ...

  3. 如何在Window下安装node\npm\cnpm,并安装vue.js,创建项目

    1.安装node.js node.js的官方地址为:https://nodejs.org/en/download/. 根据windows版本后,选择要下载的安装包,下载完毕,按照windows一般应用 ...

  4. iOS开发多线程之GCD

    Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适当的Dispatch Que ...

  5. python数据类型小测试

    # 1. 有两个列表,其中一个列表a,每一项映射到另一个列表b每一项,先对a排序,要求b的中映射关系位置保持不变,给b也按照排序, (b的8对应a的[1,2], 7对应[3,4] ... ) a = ...

  6. Javascript我学之三函数的参数

    本文是金旭亮老师网易云课堂的课程笔记,记录下来,以供备忘 函数的参数             对于参数值,JavaScript不会进行类型检查,任何类型的值都可以被传递给参数.             ...

  7. python中的while循环和for循环

    1.while循环 Gif 演示 Python while 语句执行过程 while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,bre ...

  8. promise async

    最简用  promise let res = function () { return new Promise((resolve, reject) => { // 返回一个promise set ...

  9. RedisHelper帮助类

    using Newtonsoft.Json; using RedLockNet.SERedis; using RedLockNet.SERedis.Configuration; using Stack ...

  10. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-11项目日志解决方案

    本文目录1. Net下日志记录2. NLog的使用    2.1 添加nuget引用NLog.Web.AspNetCore    2.2 配置文件设置    2.3 依赖配置及调用    2.4 日志 ...