jslib-tools
Version:
js工具库 封装常用的工具函数 如深拷贝 时间转换日期格式化、浏览器判断等,提高开发效率
104 lines (85 loc) • 6.58 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: dateFormat/dateFormat1.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: dateFormat/dateFormat1.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*
* @Author: zhangyu
* @Email: zhangdulin@outlook.com
* @Date: 2021-06-08 11:30:40
* @LastEditors: zhangyu
* @LastEditTime: 2021-06-10 17:55:31
* @Description:
*/
/**
* 日期格式化
* 时间规则:
* 今天显示:xx:xx
* 昨天显示:昨天 xx:xx
* 一周内显示:星期x xx:xx
* 一年内显示:xx月xx日 xx:xx
* 更早日期显示:xx年xx月xx日 xx:xx
* @param {*} value 时间戳,单位s
* @return {*} 格式化后的日期
*/
export function dateFormat1(value) {
if (!value) return "";
const time = value * 1000;
const now = new Date().getTime();
const year = new Date(time).getFullYear();
const month = new Date(time).getMonth() + 1;
const date = new Date(time).getDate();
let hour = new Date(time).getHours();
let min = new Date(time).getMinutes();
const weekType = ["日", "一", "二", "三", "四", "五", "六"];
const week = "星期" + weekType[new Date(time).getDay()];
if (min < 10) {
min = "0" + min;
}
if (hour < 10) {
hour = "0" + hour;
}
if (new Date().getDate() - new Date(time).getDate() === 0) {
return hour + ":" + min;
} else if (new Date().getDate() - new Date(time).getDate() === 1) {
return "昨天 " + hour + ":" + min;
} else if (
new Date().getTime() < new Date(time).getTime() &&
new Date().getDate() - new Date(time).getDate() < 7 &&
new Date().getDay() - new Date(time).getDay() > 0
) {
return week + hour + ":" + min;
} else if (year < new Date(now).getFullYear()) {
return year + "年" + month + "月" + date + "日 " + hour + ":" + min;
} else {
return month + "月" + date + "日 " + hour + ":" + min;
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#alias">alias</a></li><li><a href="global.html#arrayMove">arrayMove</a></li><li><a href="global.html#arrayMoveMutate">arrayMoveMutate</a></li><li><a href="global.html#arrayToObject">arrayToObject</a></li><li><a href="global.html#arrayToTree">arrayToTree</a></li><li><a href="global.html#arrFibonacci">arrFibonacci</a></li><li><a href="global.html#arrVals">arrVals</a></li><li><a href="global.html#callHandler">callHandler</a></li><li><a href="global.html#camelize">camelize</a></li><li><a href="global.html#checkInt">checkInt</a></li><li><a href="global.html#checkNum">checkNum</a></li><li><a href="global.html#clipboardObj">clipboardObj</a></li><li><a href="global.html#compareSize">compareSize</a></li><li><a href="global.html#cookie">cookie</a></li><li><a href="global.html#cursortPosition">cursortPosition</a></li><li><a href="global.html#dasherize">dasherize</a></li><li><a href="global.html#dateFormat1">dateFormat1</a></li><li><a href="global.html#dateFormat2">dateFormat2</a></li><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#decode_url_param">decode_url_param</a></li><li><a href="global.html#def">def</a></li><li><a href="global.html#elDateFormat">elDateFormat</a></li><li><a href="global.html#escapeHTML">escapeHTML</a></li><li><a href="global.html#exportXls">exportXls</a></li><li><a href="global.html#extend">extend</a></li><li><a href="global.html#getBrowserModel">getBrowserModel</a></li><li><a href="global.html#getChangedData">getChangedData</a></li><li><a href="global.html#getChildren">getChildren</a></li><li><a href="global.html#getCookie">getCookie</a></li><li><a href="global.html#getDefaultAvatar">getDefaultAvatar</a></li><li><a href="global.html#getDeviceModel">getDeviceModel</a></li><li><a href="global.html#getHttpBase64">getHttpBase64</a></li><li><a href="global.html#getImgBase64">getImgBase64</a></li><li><a href="global.html#getThumbnails">getThumbnails</a></li><li><a href="global.html#getType">getType</a></li><li><a href="global.html#handleEmoji">handleEmoji</a></li><li><a href="global.html#handleParam">handleParam</a></li><li><a href="global.html#handleText">handleText</a></li><li><a href="global.html#isArray">isArray</a></li><li><a href="global.html#isArrayLike">isArrayLike</a></li><li><a href="global.html#isDiff">isDiff</a></li><li><a href="global.html#isFunc">isFunc</a></li><li><a href="global.html#isInt">isInt</a></li><li><a href="global.html#isNum">isNum</a></li><li><a href="global.html#isObject">isObject</a></li><li><a href="global.html#isObjEmpty">isObjEmpty</a></li><li><a href="global.html#isUInt">isUInt</a></li><li><a href="global.html#isUNum">isUNum</a></li><li><a href="global.html#makeArray">makeArray</a></li><li><a href="global.html#objectConversionToList">objectConversionToList</a></li><li><a href="global.html#objectToArray">objectToArray</a></li><li><a href="global.html#objTools">objTools</a></li><li><a href="global.html#parseTime">parseTime</a></li><li><a href="global.html#setVideoPlay">setVideoPlay</a></li><li><a href="global.html#sleepAction">sleepAction</a></li><li><a href="global.html#sleepSync">sleepSync</a></li><li><a href="global.html#sortMapByKey">sortMapByKey</a></li><li><a href="global.html#storage">storage</a></li><li><a href="global.html#throttle">throttle</a></li><li><a href="global.html#titleize">titleize</a></li><li><a href="global.html#toArray">toArray</a></li><li><a href="global.html#toLower">toLower</a></li><li><a href="global.html#toMap">toMap</a></li><li><a href="global.html#toObjectMap">toObjectMap</a></li><li><a href="global.html#toSimplifiedChinese">toSimplifiedChinese</a></li><li><a href="global.html#toUpper">toUpper</a></li><li><a href="global.html#unescapeHTML">unescapeHTML</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Wed Aug 25 2021 17:57:45 GMT+0800 (GMT+08:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>