字符串方法以及时间对象/JS三

字符串方法

根据下标返回字符 charAt()

var str = 'asdef'
alert(str.charAt(3))  //结果为:f
alert(str.charCodeAt(3))  //返回下标位置字符串的ASCII码

连接字符串concat('hhh','kkk')

var str1=str.concat('hhh','kkk')
console.log(str1)  //asdfhhhkkk
console.log(str+"asd")

根据字符串查找下标  indexOf()

console.log(str.indexOf('de'))  //结果为:2 。找子串,返回子串的下标。查找失败返回-1
console.log(str.indexOf('de',3))  //结果为:-1

截取子串.slice
var  str2="www.sdfsfd.com?name='ll'&&age=18"
var askIndex=str2.indexOf('?')
dat1=str2.slice(askIndex+1)//提取问号后的数据

将字符串转为数组 split('分隔符')
dat1=str2.slice(askIndex+1)//提取问号后的数据
alert(dat1)
var b=dat1.split('&&')
var obj={}
for (var i=0;i<b.length;i++){
    obj[shuxm]=b[i].split('=')[1];
}
alert(obj)

时间对象

<script>
    var time=new Date()
    console.log(time.valueOf())  //返回70年到现在的毫秒数,不是秒数
    console.log(time.getDate())   //返回70年到现在的毫秒数,不是秒数
    //2019-03-27  11:29
    function formatTime() {
        var T=new Date()
        var year=T.getFullYear()//获取年
        var mouth=T.getMonth()+1  //获取月,从0月开始
        var day=T.getDate()        //获取日,从1开始
        var minutes=T.getMinutes() //分
        var seconds=T.getSeconds() //秒

        console.log(year,mouth,day,minutes,seconds)
        return year+'-'+(mouth>10?month:('0'+mouth)) +'-'+(day>10?day:('0'+day))

    }
    formatTime()



</script>

猜你喜欢

转载自blog.csdn.net/qq_39112101/article/details/88857042