zoukankan      html  css  js  c++  java
  • 动态判断时间插件显示到年月日时分秒

    根据得到的时间格式,动态判断时间插件显示到年月日时分秒

    主要split切割字符串来判断
    split将一个字符串切割为字符串数组

    <!DOCTYPE html>
    <html>
    
    	<head>
    		<meta charset="UTF-8">
    		<title>split切割数组</title>
    	</head>
    
    	<body>
    		<script type="text/javascript">
    			let time = '2018-10-19 12:30' //现在的时间格式
    			let formatStr = "YYYY-MM-DD"   //定义一个默认的时间格式
    
    			const arr =time.split(' ')  //将拿到的时间格式按照空格用split切割
    			const arrLen = arr.length
    			if(arrLen >= 2) {   //数组的长度大于等于2      证明有时分
    				const hhArr = arr[1].split(":")  //将数组的第二项按冒号分割  大于1 到分钟 等于1是到小时
    				const hhArrLen = hhArr.length
    				if(hhArrLen > 1) {
    					formatStr += " " + "HH:mm"
    				} else if(hhArrLen === 1) {
    					formatStr += " " + "HH"
    				}
    			} else if(arrLen === 1) {  //数组的长度等于1  再按照-切割 ,得到的字符串数组  长度为三是年  为二是月  为一是年
    				const yyArr = arr[0].split("-")
    				const yyArrLen = yyArr.length
    				if(yyArrLen === 3) {  
    					formatStr = "YYYY-MM-DD"
    				} else if(yyArrLen === 2) {
    					formatStr = "YYYY-MM"
    				} else if(yyArrLen === 1) {
    					formatStr = "YYYY"
    				}
    			}
    		</script>
    
    	</body>
    
    </html>
    
  • 相关阅读:
    Pick-up sticks
    The Doors
    Intersecting Lines
    Segments
    TOYS
    Palindrome
    Distinct Substrings
    Milk Patterns
    Musical Theme
    JavaScript基于时间的动画算法
  • 原文地址:https://www.cnblogs.com/lml-lml/p/9810370.html
Copyright © 2011-2022 走看看