美文网首页
数组、字符串、日期常用操作

数组、字符串、日期常用操作

作者: 小米和豆豆 | 来源:发表于2021-03-29 14:33 被阅读0次
  1. 字符串
    var a = 'string'
    console.log(a.slice(2, 4)) //截取下标 2,3
    console.log(a.substring(2, 4)) //截取下标 2, 3
    console.log(a.substr(2, 4)) //2开始,取4位
        // a.lastIndexOf()返回字符串中指定字符最后一次出现的位置
    console.log(a.split('')) //转数组
      // toUpperCase()把字母转换为大写
      // toLowerCase()把字母转换为小写
  1. 数组
    var b = ['a', 'b', 'c', 'd', 'f', 'g'];
    console.log(b.slice(2, 4)) //截取 下标 2,3 新数组 [c,d]
    console.log(b) //不改变原数组

    console.log(b.splice(3, 1, 'adf')) //可3个参数 开始,数量,替换元素 把d换为adf
    console.log(b) //改变原数组

    console.log(b.join(',')) //转字符串
    console.log(b) //不变原数组
  1. 日期
var d= new Date(year, month, 0).getDate()   //某月天数

/**
 * @description: 格式化日期
 * @param {*} formatStr 日期格式
 */
Date.prototype.Format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
    var month = this.getMonth() + 1;
    str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
    str = str.replace(/M/g, month);

    str = str.replace(/w|W/g, Week[this.getDay()]);

    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());

    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());

    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());
    return str;
}
console.log(new Date().Format("yyyy/MM/dd/星期w hh:mm:ss"));

相关文章

  • 数组常用 API

    数组常用 api 介绍 split():拆分字符串变成数组(对字符串操作)join() :合并数组变成字符串(对数...

  • js基础了解

    js数组常用遍历方法使用: js数组常用操作方法使用: 基本逻辑运算: 基本字符串操作方法:

  • JavaScript 基础

    数据类型: 日期对象: 运算符: 定时器: 字符串常用方法: 数组常用方法: Math常用方法: i的问题(即执行...

  • #python入门5#高级特性--切片

    切片类似 php的截取(字符串/数组截取) 常用于list, tupple,字符串的操作 L = ['Michae...

  • 常用排序算法

    7、字符串的常用操作: Char[] c = 字符串.toCharArray() ; //将一个字符串变为字符数组...

  • J S

    导入 1 基础语法 2 DOM操作(必须会 熟练操作 ) 3 日期对象 字符串处理 数组处理正侧 -------...

  • Swift-字符串,数组,字典

    摘要:简单介绍下在Swift中对 字符串,数组,字典的常用操作。 字符串 字符串的长度 字符串的拼接 字符串的截取...

  • php常用日期操作函数以及周操作和月操作的坑

    php开发中经常用到的日期操作函数: 天、时、分、秒操作 strtotime 第一个参数为日期格式化字符串,默认第...

  • JavaSE第13篇:常用API、冒泡排序、二分查找、正则

    核心概述:本篇我们将会学习常用的API,其中有Object类、日期相关操作类、数组相关操作类Arrays、正则表达...

  • js中字符串和数组的常用方法

    字符串的常用方法 数组的常用方法

网友评论

      本文标题:数组、字符串、日期常用操作

      本文链接:https://www.haomeiwen.com/subject/vicihltx.html