JavaScript字符串部分操作方法(split,indexOf,moment,new Date)

1、split() 

​  split() 方法可用于把一个字符串分割成字符串数组。例子中使用'.xml',把一个字符以’.xml‘为界限分为两部分。

let name = 'addressbook(1).xml'
let nameSplit = name.split(xml) //nameSplit = ['addressbook(1)', '']

其他常见应用场景

① 分割参数:split() 
let rows= "aa,bb,cc"
let str = rows.split()
console.log(str) //['aa,bb,cc']
② 分割每个字符:split("")
let rows= "aa, bb,cc"
let	str2 = rows.split("")
console.log(str2)// ['a', 'a', ',', ' ', 'b', 'b', ',', 'c', 'c']

2、indexOf()

        indexOf() 方法可返回数组或字符串中某个指定的元素位置,返回元素第一次出现的位置。如果在数组中没找到指定元素则返回 -1,查找区分大小写。由返回-1可以用于判断,数组或字符串中是否含有某个元素字符。

for example 1:查找数组中的字符

    const arry = ref<any[]>(['abc','Abc','junk','🍟',])   
    console.log(arry.value.indexOf('Abc')) //1  区分大小写
    console.log(arry.value.indexOf('111')) //-1
    console.log(arry.value.indexOf('🍟')) //3

for example 2:查找字符串中的字符

    const arry1 = ref<any>('a我是薯条Acd')    
    console.log(arry1.value.indexOf('🍟')) // -1
    console.log(arry1.value.indexOf('薯条')) // 3 返回第一个字符的位置index
    console.log(arry1.value.indexOf('a')) //0
    console.log(arry1.value.indexOf('A')) //5 区分大小写

3、moment插件

用于获取时间戳。

安装和引入

npm install moment --save //  安装
yarn add moment 

import moment from 'moment' //引入

不同使用场景

① 生成指定时间的moment对象

moment() :不传参数,默认返回当前时间的moment对象

moment("time") :返回指定的时间time的moment对象
moment("time","MM-DD-YYYY") :moment对象的格式为MM-DD-YYYY

 moment对象:

moment('2024-04-01')

② 直接获取时间戳。

moment().valueOf() :获取时间精确到毫秒 
moment().format('x') :时间精确到毫秒且返回值为字符串类型 
moment().unix() :获取时间精确到秒 

    console.log(moment().valueOf()) // 1730427978724
    console.log(moment().format('x')) // '1730427978724'
    console.log(moment().unix()) //1730427978
③ 获取包括时间参数的对象

moment().toObject()

console.log(moment().toObject())

返回一个包括:年、月、日、时、分、秒、毫秒的对象

④ 对时间进行格式化

在element时间组件中获取到的时间往往是默认格式,需要指定格式返回出正常格式的时间。

moment().format() :返回默认格式的时间

moment().format('YYYY-MM-DD HH:mm:ss') :返回指定格式的时间

    console.log(moment().format()) //2024-11-01T11:19:10+08:00
    console.log(moment().format('YYYY-MM-DD HH:mm:ss')) //2024-11-01 11:19:10
⑤ 时间戳格式化转换

moment.unix(时间戳).format(“YYYY-MM-DD HH:mm”) 时间戳转年月日格式

console.log(moment.unix(1730427978).format('YYYY-MM-DD HH:mm:ss') )
//2024-11-01 10:26:18
⑥ 获取其他对应时间.....
moment.year()  // 年份
moment.month()  // 月份
moment.date()  // 日
moment.hour()  // 小时
moment.minute()  // 分钟
moment.second()  // 秒

4、实例化时间对象new Date()获取/操作时间戳

①:返回当前的本地日期和时间

new Date()  返回一个表示本地日期和时间的Date对象

②:获得时间戳(与'1970/01/01 00:00:00'之间的毫秒值)

Date.parse(dateStr)

dateStr:时间字符串,字符串的格式主要有两种。

1) yyyy/MM/dd HH:mm:ss 

2) yyyy-MM-dd HH:mm:ss 

console.log(Date.parse(new Date()))  
//获取当前时间的时间戳  1730443432000
console.log(Date.parse('2024-11-01 10:26:18'))  
//获取2024-11-01 10:26:18的时间戳  1730427978000
③把时间戳转化为YYYY-MM-DD hh:mm:ss格式

转化方法:


function translateTime(time) {
        let date = new Date(time);//时间戳为10位需*1000(time * 1000)
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1):date.getMonth()+1) + '-';
        let D = (date.getDate()< 10 ? '0'+date.getDate():date.getDate())+ ' ';
        let h = (date.getHours() < 10 ? '0'+date.getHours():date.getHours())+ ':';
        let m = (date.getMinutes() < 10 ? '0'+date.getMinutes():date.getMinutes()) + ':';
        let s = date.getSeconds() < 10 ? '0'+date.getSeconds():date.getSeconds();
        return Y+M+D+h+m+s;
}

    let a = translateTime(Date.parse(new Date()))
    console.log(a) //2024-11-01 15:15:18
④获取其他对应时间....

new Date().getYear(); //获取当前年份(2位)
new Date().getFullYear(); //获取完整的年份(4位,1970-????)
new Date().getMonth(); //获取当前月份(0-11,0代表1月)
new Date().getDate(); //获取当前日(1-31)
new Date().getDay(); //获取当前星期X(0-6,0代表星期天)
new Date().getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
new Date().getHours(); //获取当前小时数(0-23)
new Date().getMinutes(); //获取当前分钟数(0-59)
new Date().getSeconds(); //获取当前秒数(0-59)
new Date().getMilliseconds(); //获取当前毫秒数(0-999)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值