龙空技术网

JS的时间操作long转成字符串显示

符志雄 70

前言:

此刻看官们对“js怎么将字符串转换成日期”可能比较注意,你们都想要剖析一些“js怎么将字符串转换成日期”的相关知识。那么小编同时在网摘上收集了一些有关“js怎么将字符串转换成日期””的相关知识,希望咱们能喜欢,同学们快快来了解一下吧!

界面效果:

关键代码:

function dateShowByLong(dateTimeValue) {	if (dateTimeValue == null ||		dateTimeValue == "" ||		dateTimeValue == undefined) {		return "";	}	let dateV = new Date();	dateV.setTime(dateTimeValue);	let result = dateFormat(dateV, "yyyy-MM-dd hh:mm:ss");	return result;}function dateFormat(date, fmt) {	// 时间格式化输出 //	// date:时间值 //	// fmt:格式化方式:如:yyyy-MM-dd //	let o = {		"M+": date.getMonth() + 1, //月份		"d+": date.getDate(), //日		"h+": date.getHours(), //小时		"m+": date.getMinutes(), //分		"s+": date.getSeconds(), //秒		"q+": Math.floor((date.getMonth() + 3) / 3), //季度		"S": date.getMilliseconds() //毫秒	};	if (/(y+)/.test(fmt))		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));	for (let k in o) {		if (new RegExp("(" + k + ")").test(fmt)) {			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr((				"" + o[k]).length)));		}	}	return fmt;}

在线验证链接:

「链接」

完整代码(可忽略不看):

<table border="0" class="tool-one-line" width="99%">	<tr>		<td align="right" style="width: 150px;">		</td>		<td colspan="1">			JS的时间操作long转成字符串显示		</td>	</tr>	<tr>		<td align="right" style="width: 150px;">输入:		</td>		<td colspan="1">			<textarea name="txt_input" rows="2" cols="5" id="txt_input"				style="height: 100px; width: 98%;"></textarea>		</td>	</tr>	<tr>		<td align="right" style="width: 150px; height: 35px;"> 		</td>		<td colspan="1">			<input type="button" id="btn_ok" class="tool-btn" value="输出" action-name="ok"				onclick="btnClick(this);" />			|			<input type="button" id="btn_example" class="tool-btn" value="样例" onclick="exampleClick();" />		</td>	</tr>	<tr>		<td align="right" valign="top" rowspan="2">输出:<br />			<input type="button" id="btn_copy" class="tool-btn" value="复制" onclick="onCopyClick();" />		</td>		<td colspan="3">			<span id="span_execTime"></span>		</td>	</tr>	<tr>		<td colspan="3" valign="top">			<textarea name="txt_result" rows="2" cols="5" id="txt_result"				style="height: 100px; width: 98%;"></textarea>		</td>	</tr></table><script language="javascript" type="text/javascript">		let resultData = null;	function onCopyClick() {		webUtil.htmlFn.copyTextById("txt_result", "span_execTime");	}	function exampleClick() {		let result = "";		result += "1705750623832\n";		result += "1701750623811\n";		result += "1601150621811\n";		result += "";		$("#txt_input").val(result);	}	function dateShowByLong(dateTimeValue) {		if (dateTimeValue == null ||			dateTimeValue == "" ||			dateTimeValue == undefined) {			return "";		}		let dateV = new Date();		dateV.setTime(dateTimeValue);		let result = dateFormat(dateV, "yyyy-MM-dd hh:mm:ss");		return result;	}	function dateFormat(date, fmt) {		// 时间格式化输出 //		// date:时间值 //		// fmt:格式化方式:如:yyyy-MM-dd //		let o = {			"M+": date.getMonth() + 1, //月份			"d+": date.getDate(), //日			"h+": date.getHours(), //小时			"m+": date.getMinutes(), //分			"s+": date.getSeconds(), //秒			"q+": Math.floor((date.getMonth() + 3) / 3), //季度			"S": date.getMilliseconds() //毫秒		};		if (/(y+)/.test(fmt))			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));		for (let k in o) {			if (new RegExp("(" + k + ")").test(fmt)) {				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr((					"" + o[k]).length)));			}		}		return fmt;	}	function onClick(oThis) {		let inputA = $("#txt_input").val().split('\n');		let result = "";		let iLen = inputA.length;		let i = 0;		let item = null;		for (i = 0; i < iLen; ++i) {			item = inputA[i];			if (item == null) {				continue;			}			item = $.trim(item);			if (item == "") {				continue;			}			item = dateShowByLong(item);			result += item + '\n';		}		return result;	}	function btnClick(oThis) {		let actionName = $(oThis).attr("action-name");		let result = null;		switch (actionName) {			case "ok":				result = onClick(oThis);				break;			case "":			default:				break;		}		if (result != null) {			$("#txt_result").val(result);		}		$("#span_execTime").html(webUtil.dateFn.showDateTimeNow());	}</script>	

标签: #js怎么将字符串转换成日期 #js如何将字符串转为日期