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

今天的时间,统一用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. Angular笔记-select

    --select-- 设置默认选中值方法: <select ng-model="url" ng-options="x.url as x.site for x in ...

  2. H2 Database Engine

    http://www.h2database.com/html/main.html H2 Database Engine Welcome to H2, the Java SQL database. Th ...

  3. Flutter终将逆袭!1.2版本发布,或将统一江湖

    在去年 MWC 大展上发布首个 Beta 版后,Flutter 1.0 正式版于 2018 年 12 月召开的 Flutter Live 2018 上正式发布.今天在巴塞罗那召开的 MWC 发布会上, ...

  4. uni-app调用原生的文件系统管理器(可选取附件上传)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. Java_日期时间相关类

    目录 Date类(java.util.date) Calendar类(java.util.Calendar) SimpleDateFormat类(java.text.SimpleDateFormat) ...

  6. 团队项目——NABCD

    团队名称:天码行空 团队项目名称:天码记账 团队口号:彰明往事,考察未来 发布后一周内用户量:1千 N(Need)需求 这个软件主要解决了大学生管理自己财务状况的问题,随着手机支付的日趋流行大家对财务 ...

  7. 论文阅读笔记四十一:Very Deep Convolutional Networks For Large-Scale Image Recongnition(VGG ICLR2015)

    论文原址:https://arxiv.org/abs/1409.1556 代码原址:https://github.com/machrisaa/tensorflow-vgg 摘要 本文主要分析卷积网络的 ...

  8. 微信小程序换皮肤,动态切换菜单栏和导航栏的样式,动态修改TabBar和NavigationBar

    在做微信小程序换皮肤的时候,需要动态修改菜单栏(TabBar)和导航栏(NavigationBar) 但是在小程序中它们的样式是写在app.json里面,而且app.json是静态编译,运行时哪怕你修 ...

  9. mysql根据分组和条件查询以后如何统计记录的条数

    1.子查询,查询出的数据随便起一个别名,然后根据分组和条件查询出的数据,作为一个具有一列的一个表,然后外面的查询查询这个数据表的这一列的总数,即可. SELECT COUNT( * ) FROM ( ...

  10. docker 搭建以太坊私有链搭建

    最近区块链,火得不行,身边也有朋友准备玩这个,说是搭了一个星期,没有把环境搭建起来,叫我帮忙看看环境怎么搭建 于是我找到了官方的地址 https://github.com/ethereum/go-et ...