首页 > 编程语言 >微信小程序上时间转换格式

微信小程序上时间转换格式

时间:2022-11-24 14:12:30浏览次数:49  
标签:转换 log 微信 console var 2022 date 格式 day

  • utils/utils.ts

日期格式转换

      /**
      * 时间戳转化为年 月 日 时 分 秒
      * @params number 传入时间戳
      * @params format:返回格式,支持自定义,但参数必须与formateArr里保持一致
      * formatTime(sjc,'Y/M/D h:m:s')
      */
      export function formatTime(_Date: any, format: any = "Y/M/D") {
        if (!_Date) {
          return "";
        }
        var date = new Date(_Date);
        return commonDate(date,format)
      }

      const commonDate = (date: any, format = "Y/M/D") => {
        var formateArr = ["Y", "M", "D", "h", "m", "s"];
        var returnArr = [];
        returnArr.push(date.getFullYear());
        returnArr.push(formatNumber(date.getMonth() + 1));
        returnArr.push(formatNumber(date.getDate()));
        returnArr.push(formatNumber(date.getHours()));
        returnArr.push(formatNumber(date.getMinutes()));
        returnArr.push(formatNumber(date.getSeconds()));
        let timeStr = format;
        for (var i in formateArr) {
          timeStr = timeStr.replace(formateArr[i], returnArr[i]);
        }
        return timeStr;
      }

      const formatNumber = (n: number) => {
        const s = n.toString()
        return s[1] ? s : '0' + s
      }
      const  formatDate = (date: any, format = "Y/M/D") => {
        return commonDate(date,format)
      }
        // 【JS】js将 /Date(1669257627432+0800)/ 转换为YYYY-MM-DD格式
      export function formatDateStamp(time?: any, format?:any) {
        if (!time) {
          return ''
        }
        var t = time.slice(6, 19)
        var NewDtime = new Date(parseInt(t));
        return formatDate(NewDtime, format );
      }
  • 使用

  • 默认显示 YYYY/MM/DD 格式

        import { formatDateStamp,formatTime } from "../../utils/util";
    
        console.log(formatDateStamp('/Date(1669257627432+0800)/')) // 2022/11/24
        console.log(formatDateStamp('/Date(1669257627432+0800)/','Y-M-D h:m:s')) // 2022-11-24 10:40:27
        console.log(formatTime('2022-10-13T12:30:43.73')) // 2022/10/13
        console.log(formatTime('2022-10-13T12:30:43.73','Y-M-D h:m:s')) // 2022-10-13 12:30:43
        console.log(formatTime('2022-10-13T12:30:43.73','Y/M/D h:m:s')) // 2022/10/13 12:30:43
        console.log(formatTime(1669267989009)) // 2022/11/24
        console.log(formatTime(1669267989009,'Y-M-D h:m:s')) // 2022-11-24 13:33:09
    

日期转换为周几

  • 方法

      export function getWeekByDate ( dates:any)  {
        let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
        let date = new Date(dates);
        date.setDate(date.getDate());
        let day = date.getDay();
        return show_day[day];
      }
    
  • 使用

      console.log(getWeekByDate('2022-11-24')) // 周四
      console.log(getWeekByDate('2022/11/24')) // 周四
      console.log(getWeekByDate('2022-10-13T12:30:43.73')) // 周四
    

获取未来的天数的完整信息

  • 方法

      export function dealTime (num: any)  {     // num:未来天数
        var time = new Date()     // 获取当前时间日期
        var date = new Date(time.setDate(time.getDate() + num)).getDate()  //这里先获取日期,在按需求设置日期,最后获取需要的
        var year = time.getFullYear()
        var month = time.getMonth() + 1   // 获取月份
        var day: any = time.getDay()   //  获取星期
        switch (day) {             //  格式化
          case 0: day = "周日"
            break
          case 1: day = "周一"
            break
          case 2: day = "周二"
            break
          case 3: day = "周三"
            break
          case 4: day = "周四"
            break
          case 5: day = "周五"
            break
          case 6: day = "周六"
            break
        }
        var month1 =  month < 10 ? "0" + month :  month
        var date1 = date < 10 ? "0" + date : date
        var obj = {
          year: year,
          month: month,
          date: date,
          day: day,
          newday: year + '/' + month1 + '/' + date1,
        }
        // 安卓可以处理‘-’与‘/’的时间格式,而IOS系统上只能处理‘/’的时间格式
        return obj      // 返回对象
      }
    
  • 使用

        console.log(dealTime(0)) // 今天 { date: 24,day: "周四",month: 11,newday: "2022/11/24",year: 2022 }
        console.log(dealTime(1)) // 明天 { date: 25,day: "周五",month: 11,newday: "2022/11/25",year: 2022 }
        console.log(dealTime(7)) // { year: 2022, month: 12, date: 1, day: "周四", newday: "2022/12/01" }
    

Date.parse

  • 日期比较大小

      console.log(Date.parse('2022-11-24') <= Date.parse('2022-11-1')) // false
      console.log(Date.parse('2022-11-01') <= Date.parse('2022-11-14')) // true

标签:转换,log,微信,console,var,2022,date,格式,day
From: https://www.cnblogs.com/DCL1314/p/16921665.html

相关文章