In the past, mail was very slow and people only had to play one job in their whole life. Now in the age of rapid development of information technology, how can we workers slow down and let us unite as one and keep our heads down and work hard

Formatting time

  • The 1970-01-01 08:00:00 Custom format
/ * * *@description Formatting time@param time
 * @param cFormat
 * @returns {string|null}- the 2008-07-22 22:49:41 * /
 
 export function parseTime(time, cFormat) {
	if (arguments.length === 0) {
		return null
	}
	const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if (typeof time === 'string' && / ^ [0-9] + $/.test(time)) {
			time = parseInt(time)
		}
		if (typeof time === 'number' && time.toString().length === 10) {
			time = time * 1000
		}
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1.d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay(),
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g.(result, key) = > {
		let value = formatObj[key]
		if (key === 'a') {
			return ['day'.'一'.'二'.'三'.'four'.'five'.'六'][value]
		}
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}
Copy the code
  • Just a few minutes ago a few hours ago It’s eight o ‘clock on January 1
export function formatTime(time, option) {
	if ((' ' + time).length === 10) {
		time = parseInt(time) * 1000
	} else {
		time = +time
	}
	const d = new Date(time)
	const now = Date.now()

	const diff = (now - d) / 1000

	if (diff < 30) {
		return 'just'
	} else if (diff < 3600) {
		// less 1 hour
		return Math.ceil(diff / 60) + 'Minutes ago'
	} else if (diff < 3600 * 24) {
		return Math.ceil(diff / 3600) + 'Hours ago'
	} else if (diff < 3600 * 24 * 2) {
		return 'one day before'
	}
	if (option) {
		return parseTime(time, option)
	} else {
		return (
			d.getMonth() +
			1 +
			'month' +
			d.getDate() +
			'day' +
			d.getHours() +
			'when' +
			d.getMinutes() +
			'points')}}Copy the code
  • Good morning, good afternoon, good evening
export function timeFix () {
  const time = new Date(a)const hour = time.getHours()
  return hour < 9 ? 'Good morning' : hour <= 11 ? 'Good morning' : hour <= 13 ? 'Good afternoon' : hour < 20 ? 'Good afternoon' : 'Good evening'
}
Copy the code
  • 10 bit timestamp conversion
export function tenBitTimestamp(time) {
	const date = new Date(time * 1000)
	const y = date.getFullYear()
	let m = date.getMonth() + 1
	m = m < 10 ? ' ' + m : m
	let d = date.getDate()
	d = d < 10 ? ' ' + d : d
	let h = date.getHours()
	h = h < 10 ? '0' + h : h
	let minute = date.getMinutes()
	let second = date.getSeconds()
	minute = minute < 10 ? '0' + minute : minute
	second = second < 10 ? '0' + second : second
	return y + 'years' + m + 'month' + d + 'day' + h + ':' + minute + ':' + second
}
Copy the code
  • 13 bit timestamp conversion
export function thirteenBitTimestamp(time) {
	const date = new Date(time / 1)
	const y = date.getFullYear()
	let m = date.getMonth() + 1
	m = m < 10 ? ' ' + m : m
	let d = date.getDate()
	d = d < 10 ? ' ' + d : d
	let h = date.getHours()
	h = h < 10 ? '0' + h : h
	let minute = date.getMinutes()
	let second = date.getSeconds()
	minute = minute < 10 ? '0' + minute : minute
	second = second < 10 ? '0' + second : second
	return y + 'years' + m + 'month' + d + 'day' + h + ':' + minute + ':' + second
}
Copy the code

Convert url request parameters to JSON format

Example: HTTP:/ / 127.0.0.1:8080 / HTML/urltojson. HTML? id=1&name=good#&price=1234Before we get into the subject, let me look at the method of obtaining the URL:window.location.href // Set or get the entire URL as a string -->
window.location.hash // Set or get the href parameter after the hash # --> #&price=1234
window.location.search // Set or get the href attribute following the question mark? After, the parameter before hash # -->? id=1&name=good
Copy the code
/ * * *@description Convert url request parameters to JSON format *@param url
 * @returns {id: "1", name: "good#", price: "1234"}* /
export function paramObj(url) {
	const search = url.split('? ') [1]
	if(! search) {return{}}return JSON.parse(
		'{"' +
		decodeURIComponent(search)
		.replace(/"/g.'\ \ "')
		.replace(/&/g.'",")
		.replace(/=/g.'" : "')
		.replace(/\+/g.' ') +
		'"}')}Copy the code

An array of parent-child relationships is converted to tree-structured data

Of course, in real development, the definition of the property name may be different from that in my example. Using this method, you need to change the property name to the actual name. The key property names are ID,parentId, and Childrens

/ * * *@description An array of parent-child relationships is converted to tree-structured data *@param data
 * @returns {*}* /
export function translateDataToTree(data) {
	const parent = data.filter(
		(value) = > value.parentId === 'undefined' || value.parentId == null
	)
	const children = data.filter(
		(value) = >value.parentId ! = ='undefined'&& value.parentId ! =null
	)
	const translator = (parent, children) = > {
		parent.forEach((parent) = > {
			children.forEach((current, index) = > {
				if (current.parentId === parent.id) {
					const temp = JSON.parse(JSON.stringify(children))
					temp.splice(index, 1)
					translator([current], temp)
					typeofparent.children ! = ='undefined' ?
						parent.children.push(current) :
						(parent.children = [current])
				}
			})
		})
	}
	translator(parent, children)
	return parent
}
Copy the code

Tree structured data is converted to parent-child arrays

/ * * *@description Tree structured data is converted to parent-child arrays *@param data
 * @returns {} []* /
export function translateTreeToData(data) {
	const result = []
	data.forEach((item) = > {
		const loop = (data) = > {
			result.push({
				id: data.id,
				name: data.name,
				parentId: data.parentId,
			})
			const child = data.children
			if (child) {
				for (let i = 0; i < child.length; i++) {
					loop(child[i])
				}
			}
		}
		loop(item)
	})
	return result
}
Copy the code