init
This commit is contained in:
		
							
								
								
									
										10
									
								
								uni_modules/uni-dateformat/changelog.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								uni_modules/uni-dateformat/changelog.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
## 1.0.0(2021-11-19)
 | 
			
		||||
- 优化 组件UI,并提供设计资源,详见:[https://uniapp.dcloud.io/component/uniui/resource](https://uniapp.dcloud.io/component/uniui/resource)
 | 
			
		||||
- 文档迁移,详见:[https://uniapp.dcloud.io/component/uniui/uni-dateformat](https://uniapp.dcloud.io/component/uniui/uni-dateformat)
 | 
			
		||||
## 0.0.5(2021-07-08)
 | 
			
		||||
- 调整 默认时间不再是当前时间,而是显示'-'字符
 | 
			
		||||
## 0.0.4(2021-05-12)
 | 
			
		||||
- 新增 组件示例地址
 | 
			
		||||
## 0.0.3(2021-02-04)
 | 
			
		||||
- 调整为uni_modules目录规范
 | 
			
		||||
- 修复 iOS 平台日期格式化出错的问题
 | 
			
		||||
@@ -0,0 +1,200 @@
 | 
			
		||||
// yyyy-MM-dd hh:mm:ss.SSS 所有支持的类型
 | 
			
		||||
function pad(str, length = 2) {
 | 
			
		||||
	str += ''
 | 
			
		||||
	while (str.length < length) {
 | 
			
		||||
		str = '0' + str
 | 
			
		||||
	}
 | 
			
		||||
	return str.slice(-length)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const parser = {
 | 
			
		||||
	yyyy: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.year, 4)
 | 
			
		||||
	},
 | 
			
		||||
	yy: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.year)
 | 
			
		||||
	},
 | 
			
		||||
	MM: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.month)
 | 
			
		||||
	},
 | 
			
		||||
	M: (dateObj) => {
 | 
			
		||||
		return dateObj.month
 | 
			
		||||
	},
 | 
			
		||||
	dd: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.day)
 | 
			
		||||
	},
 | 
			
		||||
	d: (dateObj) => {
 | 
			
		||||
		return dateObj.day
 | 
			
		||||
	},
 | 
			
		||||
	hh: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.hour)
 | 
			
		||||
	},
 | 
			
		||||
	h: (dateObj) => {
 | 
			
		||||
		return dateObj.hour
 | 
			
		||||
	},
 | 
			
		||||
	mm: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.minute)
 | 
			
		||||
	},
 | 
			
		||||
	m: (dateObj) => {
 | 
			
		||||
		return dateObj.minute
 | 
			
		||||
	},
 | 
			
		||||
	ss: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.second)
 | 
			
		||||
	},
 | 
			
		||||
	s: (dateObj) => {
 | 
			
		||||
		return dateObj.second
 | 
			
		||||
	},
 | 
			
		||||
	SSS: (dateObj) => {
 | 
			
		||||
		return pad(dateObj.millisecond, 3)
 | 
			
		||||
	},
 | 
			
		||||
	S: (dateObj) => {
 | 
			
		||||
		return dateObj.millisecond
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 这都n年了iOS依然不认识2020-12-12,需要转换为2020/12/12
 | 
			
		||||
function getDate(time) {
 | 
			
		||||
	if (time instanceof Date) {
 | 
			
		||||
		return time
 | 
			
		||||
	}
 | 
			
		||||
	switch (typeof time) {
 | 
			
		||||
		case 'string':
 | 
			
		||||
			{
 | 
			
		||||
				// 2020-12-12T12:12:12.000Z、2020-12-12T12:12:12.000
 | 
			
		||||
				if (time.indexOf('T') > -1) {
 | 
			
		||||
					return new Date(time)
 | 
			
		||||
				}
 | 
			
		||||
				return new Date(time.replace(/-/g, '/'))
 | 
			
		||||
			}
 | 
			
		||||
		default:
 | 
			
		||||
			return new Date(time)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function formatDate(date, format = 'yyyy/MM/dd hh:mm:ss') {
 | 
			
		||||
	if (!date && date !== 0) {
 | 
			
		||||
		return ''
 | 
			
		||||
	}
 | 
			
		||||
	date = getDate(date)
 | 
			
		||||
	const dateObj = {
 | 
			
		||||
		year: date.getFullYear(),
 | 
			
		||||
		month: date.getMonth() + 1,
 | 
			
		||||
		day: date.getDate(),
 | 
			
		||||
		hour: date.getHours(),
 | 
			
		||||
		minute: date.getMinutes(),
 | 
			
		||||
		second: date.getSeconds(),
 | 
			
		||||
		millisecond: date.getMilliseconds()
 | 
			
		||||
	}
 | 
			
		||||
	const tokenRegExp = /yyyy|yy|MM|M|dd|d|hh|h|mm|m|ss|s|SSS|SS|S/
 | 
			
		||||
	let flag = true
 | 
			
		||||
	let result = format
 | 
			
		||||
	while (flag) {
 | 
			
		||||
		flag = false
 | 
			
		||||
		result = result.replace(tokenRegExp, function(matched) {
 | 
			
		||||
			flag = true
 | 
			
		||||
			return parser[matched](dateObj)
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	return result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function friendlyDate(time, {
 | 
			
		||||
	locale = 'zh',
 | 
			
		||||
	threshold = [60000, 3600000],
 | 
			
		||||
	format = 'yyyy/MM/dd hh:mm:ss'
 | 
			
		||||
}) {
 | 
			
		||||
	if (time === '-') {
 | 
			
		||||
		return time
 | 
			
		||||
	}
 | 
			
		||||
	if (!time && time !== 0) {
 | 
			
		||||
		return ''
 | 
			
		||||
	}
 | 
			
		||||
	const localeText = {
 | 
			
		||||
		zh: {
 | 
			
		||||
			year: '年',
 | 
			
		||||
			month: '月',
 | 
			
		||||
			day: '天',
 | 
			
		||||
			hour: '小时',
 | 
			
		||||
			minute: '分钟',
 | 
			
		||||
			second: '秒',
 | 
			
		||||
			ago: '前',
 | 
			
		||||
			later: '后',
 | 
			
		||||
			justNow: '刚刚',
 | 
			
		||||
			soon: '马上',
 | 
			
		||||
			template: '{num}{unit}{suffix}'
 | 
			
		||||
		},
 | 
			
		||||
		en: {
 | 
			
		||||
			year: 'year',
 | 
			
		||||
			month: 'month',
 | 
			
		||||
			day: 'day',
 | 
			
		||||
			hour: 'hour',
 | 
			
		||||
			minute: 'minute',
 | 
			
		||||
			second: 'second',
 | 
			
		||||
			ago: 'ago',
 | 
			
		||||
			later: 'later',
 | 
			
		||||
			justNow: 'just now',
 | 
			
		||||
			soon: 'soon',
 | 
			
		||||
			template: '{num} {unit} {suffix}'
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	const text = localeText[locale] || localeText.zh
 | 
			
		||||
	let date = getDate(time)
 | 
			
		||||
	let ms = date.getTime() - Date.now()
 | 
			
		||||
	let absMs = Math.abs(ms)
 | 
			
		||||
	if (absMs < threshold[0]) {
 | 
			
		||||
		return ms < 0 ? text.justNow : text.soon
 | 
			
		||||
	}
 | 
			
		||||
	if (absMs >= threshold[1]) {
 | 
			
		||||
		return formatDate(date, format)
 | 
			
		||||
	}
 | 
			
		||||
	let num
 | 
			
		||||
	let unit
 | 
			
		||||
	let suffix = text.later
 | 
			
		||||
	if (ms < 0) {
 | 
			
		||||
		suffix = text.ago
 | 
			
		||||
		ms = -ms
 | 
			
		||||
	}
 | 
			
		||||
	const seconds = Math.floor((ms) / 1000)
 | 
			
		||||
	const minutes = Math.floor(seconds / 60)
 | 
			
		||||
	const hours = Math.floor(minutes / 60)
 | 
			
		||||
	const days = Math.floor(hours / 24)
 | 
			
		||||
	const months = Math.floor(days / 30)
 | 
			
		||||
	const years = Math.floor(months / 12)
 | 
			
		||||
	switch (true) {
 | 
			
		||||
		case years > 0:
 | 
			
		||||
			num = years
 | 
			
		||||
			unit = text.year
 | 
			
		||||
			break
 | 
			
		||||
		case months > 0:
 | 
			
		||||
			num = months
 | 
			
		||||
			unit = text.month
 | 
			
		||||
			break
 | 
			
		||||
		case days > 0:
 | 
			
		||||
			num = days
 | 
			
		||||
			unit = text.day
 | 
			
		||||
			break
 | 
			
		||||
		case hours > 0:
 | 
			
		||||
			num = hours
 | 
			
		||||
			unit = text.hour
 | 
			
		||||
			break
 | 
			
		||||
		case minutes > 0:
 | 
			
		||||
			num = minutes
 | 
			
		||||
			unit = text.minute
 | 
			
		||||
			break
 | 
			
		||||
		default:
 | 
			
		||||
			num = seconds
 | 
			
		||||
			unit = text.second
 | 
			
		||||
			break
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (locale === 'en') {
 | 
			
		||||
		if (num === 1) {
 | 
			
		||||
			num = 'a'
 | 
			
		||||
		} else {
 | 
			
		||||
			unit += 's'
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return text.template.replace(/{\s*num\s*}/g, num + '').replace(/{\s*unit\s*}/g, unit).replace(/{\s*suffix\s*}/g,
 | 
			
		||||
		suffix)
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,88 @@
 | 
			
		||||
<template>
 | 
			
		||||
	<text>{{dateShow}}</text>
 | 
			
		||||
</template>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
	import {friendlyDate} from './date-format.js'
 | 
			
		||||
	/**
 | 
			
		||||
	 * Dateformat 日期格式化
 | 
			
		||||
	 * @description 日期格式化组件
 | 
			
		||||
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=3279
 | 
			
		||||
	 * @property {Object|String|Number} date 日期对象/日期字符串/时间戳
 | 
			
		||||
	 * @property {String} locale 格式化使用的语言
 | 
			
		||||
	 * 	@value zh 中文
 | 
			
		||||
	 * 	@value en 英文
 | 
			
		||||
	 * @property {Array} threshold 应用不同类型格式化的阈值
 | 
			
		||||
	 * @property {String} format 输出日期字符串时的格式
 | 
			
		||||
	 */
 | 
			
		||||
	export default {
 | 
			
		||||
		name: 'uniDateformat',
 | 
			
		||||
		props: {
 | 
			
		||||
			date: {
 | 
			
		||||
				type: [Object, String, Number],
 | 
			
		||||
				default () {
 | 
			
		||||
					return '-'
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			locale: {
 | 
			
		||||
				type: String,
 | 
			
		||||
				default: 'zh',
 | 
			
		||||
			},
 | 
			
		||||
			threshold: {
 | 
			
		||||
				type: Array,
 | 
			
		||||
				default () {
 | 
			
		||||
					return [0, 0]
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			format: {
 | 
			
		||||
				type: String,
 | 
			
		||||
				default: 'yyyy/MM/dd hh:mm:ss'
 | 
			
		||||
			},
 | 
			
		||||
			// refreshRate使用不当可能导致性能问题,谨慎使用
 | 
			
		||||
			refreshRate: {
 | 
			
		||||
				type: [Number, String],
 | 
			
		||||
				default: 0
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
		data() {
 | 
			
		||||
			return {
 | 
			
		||||
				refreshMark: 0
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
		computed: {
 | 
			
		||||
			dateShow() {
 | 
			
		||||
				this.refreshMark
 | 
			
		||||
				return friendlyDate(this.date, {
 | 
			
		||||
					locale: this.locale,
 | 
			
		||||
					threshold: this.threshold,
 | 
			
		||||
					format: this.format
 | 
			
		||||
				})
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
		watch: {
 | 
			
		||||
			refreshRate: {
 | 
			
		||||
				handler() {
 | 
			
		||||
					this.setAutoRefresh()
 | 
			
		||||
				},
 | 
			
		||||
				immediate: true
 | 
			
		||||
			}
 | 
			
		||||
		},
 | 
			
		||||
		methods: {
 | 
			
		||||
			refresh() {
 | 
			
		||||
				this.refreshMark++
 | 
			
		||||
			},
 | 
			
		||||
			setAutoRefresh() {
 | 
			
		||||
				clearInterval(this.refreshInterval)
 | 
			
		||||
				if (this.refreshRate) {
 | 
			
		||||
					this.refreshInterval = setInterval(() => {
 | 
			
		||||
						this.refresh()
 | 
			
		||||
					}, parseInt(this.refreshRate))
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<style>
 | 
			
		||||
 | 
			
		||||
</style>
 | 
			
		||||
							
								
								
									
										88
									
								
								uni_modules/uni-dateformat/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								uni_modules/uni-dateformat/package.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,88 @@
 | 
			
		||||
{
 | 
			
		||||
  "id": "uni-dateformat",
 | 
			
		||||
  "displayName": "uni-dateformat 日期格式化",
 | 
			
		||||
  "version": "1.0.0",
 | 
			
		||||
  "description": "日期格式化组件,可以将日期格式化为1分钟前、刚刚等形式",
 | 
			
		||||
  "keywords": [
 | 
			
		||||
    "uni-ui",
 | 
			
		||||
    "uniui",
 | 
			
		||||
    "日期格式化",
 | 
			
		||||
    "时间格式化",
 | 
			
		||||
    "格式化时间",
 | 
			
		||||
    ""
 | 
			
		||||
],
 | 
			
		||||
  "repository": "https://github.com/dcloudio/uni-ui",
 | 
			
		||||
  "engines": {
 | 
			
		||||
    "HBuilderX": ""
 | 
			
		||||
  },
 | 
			
		||||
  "directories": {
 | 
			
		||||
    "example": "../../temps/example_temps"
 | 
			
		||||
  },
 | 
			
		||||
  "dcloudext": {
 | 
			
		||||
    "category": [
 | 
			
		||||
      "前端组件",
 | 
			
		||||
      "通用组件"
 | 
			
		||||
    ],
 | 
			
		||||
    "sale": {
 | 
			
		||||
      "regular": {
 | 
			
		||||
        "price": "0.00"
 | 
			
		||||
      },
 | 
			
		||||
      "sourcecode": {
 | 
			
		||||
        "price": "0.00"
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "contact": {
 | 
			
		||||
      "qq": ""
 | 
			
		||||
    },
 | 
			
		||||
    "declaration": {
 | 
			
		||||
      "ads": "无",
 | 
			
		||||
      "data": "无",
 | 
			
		||||
      "permissions": "无"
 | 
			
		||||
    },
 | 
			
		||||
    "npmurl": "https://www.npmjs.com/package/@dcloudio/uni-ui"
 | 
			
		||||
  },
 | 
			
		||||
  "uni_modules": {
 | 
			
		||||
    "dependencies": ["uni-scss"],
 | 
			
		||||
    "encrypt": [],
 | 
			
		||||
    "platforms": {
 | 
			
		||||
      "cloud": {
 | 
			
		||||
        "tcb": "y",
 | 
			
		||||
        "aliyun": "y"
 | 
			
		||||
      },
 | 
			
		||||
      "client": {
 | 
			
		||||
        "App": {
 | 
			
		||||
          "app-vue": "y",
 | 
			
		||||
          "app-nvue": "y"
 | 
			
		||||
        },
 | 
			
		||||
        "H5-mobile": {
 | 
			
		||||
          "Safari": "y",
 | 
			
		||||
          "Android Browser": "y",
 | 
			
		||||
          "微信浏览器(Android)": "y",
 | 
			
		||||
          "QQ浏览器(Android)": "y"
 | 
			
		||||
        },
 | 
			
		||||
        "H5-pc": {
 | 
			
		||||
          "Chrome": "y",
 | 
			
		||||
          "IE": "y",
 | 
			
		||||
          "Edge": "y",
 | 
			
		||||
          "Firefox": "y",
 | 
			
		||||
          "Safari": "y"
 | 
			
		||||
        },
 | 
			
		||||
        "小程序": {
 | 
			
		||||
          "微信": "y",
 | 
			
		||||
          "阿里": "y",
 | 
			
		||||
          "百度": "y",
 | 
			
		||||
          "字节跳动": "y",
 | 
			
		||||
          "QQ": "y"
 | 
			
		||||
        },
 | 
			
		||||
        "快应用": {
 | 
			
		||||
          "华为": "y",
 | 
			
		||||
          "联盟": "y"
 | 
			
		||||
        },
 | 
			
		||||
        "Vue": {
 | 
			
		||||
            "vue2": "y",
 | 
			
		||||
            "vue3": "y"
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								uni_modules/uni-dateformat/readme.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								uni_modules/uni-dateformat/readme.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### DateFormat 日期格式化
 | 
			
		||||
> **组件名:uni-dateformat**
 | 
			
		||||
> 代码块: `uDateformat`
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
日期格式化组件。
 | 
			
		||||
 | 
			
		||||
### [查看文档](https://uniapp.dcloud.io/component/uniui/uni-dateformat)
 | 
			
		||||
#### 如使用过程中有任何问题,或者您对uni-ui有一些好的建议,欢迎加入 uni-ui 交流群:871950839 
 | 
			
		||||
		Reference in New Issue
	
	Block a user