js自定义格式化时间戳的格式
题目为 : 写一个模块,外部调用这个模块,请求参数是时间戳,模块要求
今天的时间,统一用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自定义格式化时间戳的格式的更多相关文章
- JS代码格式化时间戳
一.[24小时制]yyyy-MM-dd HH:mm:ss new Date().toJSON() // 2019-12-13T13:12:32.265Z 通过上面的方法,基本就可以将日期格式化,然后稍 ...
- js数字格式化千分位格式
带小数点的 var a = 8462948.2453; console.log(a.toLocaleString()) //8,462,948.245 不带小数点的 num.toString().re ...
- js格式化时间戳
//js格式化时间戳,转换为时间格式 2017-1-15 4:10:15 function getLocalTime(nS) { var time = new Date(parseInt(nS) * ...
- vue.js怎样将时间戳转化为日期格式
<!-- value 格式为13位unix时间戳 --><!-- 10位unix时间戳可通过value*1000转换为13位格式 --> export function for ...
- js时间格式化函数,支持Unix时间戳
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- JS 时间函数 / 格式化时间戳
处理时间主要使用时间对象 Date , 其提供两个静态方法 Date.now() //获得当前时间戳 Date.parse() //将字符串转化成时间戳 创建对象 new Date(); // 返回当 ...
- js格式化时间 js格式化时间戳
一个js格式化时间和js格式化时间戳的例子. 代码:/** * 时间对象的格式化; */Date.prototype.format = function(format) { /* * eg:forma ...
- 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换
var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...
- js 日期格式化函数(可自定义)
js 日期格式化函数 DateFormat var DateFormat = function (datetime, formatStr) { var dat = datetime; var str ...
随机推荐
- centos7环境下apache2.2.34的编译安装
.获取apache2..34的源码包 http://archive.apache.org/dist/httpd/httpd-2.2.34.tar.gz .获取apache的编译参数 apache的编译 ...
- tomcat日志格式中的含义
%a - 远程IP地址 %A - 本地IP地址 %b - 发送的字节数,不包括HTTP头,或“ - ”如果没有发送字节 %B - 发送的字节数,不包括HTTP头 %h - 远程主机名 %H - 请求协 ...
- ubuntu 16.04 安装 kubelet、kubeadm 和 kubectl
解决了***之后,就开始K8S安装的正式旅程,本次记录 kubelet.kubeadm 和 kubectl 的安装: apt-get update && apt-get instal ...
- nginx+iis使用
一.nginx的介绍 nginx是由俄罗斯人开发的一款高性能的http和反向代理服务器,也可以用来作为邮件代理.相比较于其他的服务器,具有占用内存少,稳定性高等优势 Nginx相关地址 源码:http ...
- C# 微信开发-----微信会员卡(一)
这是微信的官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1451025283,能看懂的朋友就请不要往下看了,我是看不懂 ...
- 适合前端学习JS的网站
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript
- ECMAScript 6中数组新方法
数组的方法 数组的的大部分方法都可以实现数组的遍历. foreach方法 实现数组的遍历 const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.forEach(fu ...
- linux的时间问题
在linux系统中时间分为修改时间(modify time 简写:mtime ),访问时间(access time 简写: atime),状态修改时间(change time 简写:ctime)三种: ...
- SpringMVC概述
1. SpringMVC是什么 SpringMVC 是 Spring 框架内置的 MVC 的实现.SpringMVC 就是一个 Spring 内置的 MVC 子框架 Spring 子框架:意味着 Sp ...
- Eclipse中如何将项目转成Dynamic Web项目
项目接手前的同事是使用MyEclipse写的,拿SVN将项目导入eclipse时,web项目就会变成java项目,这时就需要修改配置文件将项目转成web,步骤如下: 1.修改项目配置文件. 要修改的项 ...