关于时间的项目可以用的js

export default {
  /*
   * 语言 
   */
  localeLanguage: window.localStorage.localeLanguage,
  /*
   * 月份名称中英文
   */
  _monthNameEn : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
  _monthNameZh : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
  /*
   * 月份名称英文全称
   */
  _monthFullNameEn : ['January','February','March','April','May','June','July','August','September','October','November','December'],
  /*
   * 星期名称中英文
   */
  _weekdayNameEn : ['Mon','Tues','Wed','Thur','Fri','Sat','Sun'],
  _weekdayNameZh : ['周一','周二','周三','周四','周五','周六','周日'],

    /*
   * 星期名称中英文
   */
  _weekdayNameEn2 : ['Sun','Mon','Tues','Wed','Thur','Fri','Sat'],
  _weekdayNameZh2 : ['周日','周一','周二','周三','周四','周五','周六'],
  
  /*
   * 当前本地时间
   */
  now() {
    return new Date()
  },
  /*
   * 每个月的天数 
   */
  daysPerMonth (year, month) {
    return (month==1)?(year%4==0?29:28):((month==0 || month==2 || month==4 || month==6 || month==7 || month==9 || month==11)?31:30)
  },
  daysPerMonthTime (date) {
    return this.daysPerMonth(date.getFullYear(), date.getMonth())
  },
  /*
   * 每天最早时间
   */
  dayFirst(date) {
    return new Date(date.toLocaleDateString())
  },
  /*
   * 每天最晚时间
   */
  dayLast(date) {
    return new Date(new Date(date.toLocaleDateString()).getTime()+24*60*60*1000-1)
  },
  /*
   * 每月最早时间
   */
  monthFirst(date) {
    return this.dayFirst(new Date(date.getFullYear(), date.getMonth(), 1))
  },
  /*
   * 每月最晚时间
   */
  monthLast(date) {
    return this.dayLast(new Date(date.getFullYear(), date.getMonth(), this.daysPerMonthTime(date)))
  },
  /*
   * 月份名称
   */
  monthName(month) {
    return this.localeLanguage==='zh'? this._monthNameZh[month] : this._monthNameEn[month]
  },
  /*
   * 星期名称
   */
  weekName(weekday) {
    return this.localeLanguage==='zh'? this._weekdayNameZh[weekday==0? 6 : weekday-1] : this._weekdayNameEn[weekday==0? 6 : weekday-1]
  },
  /*
   * 课堂上课时间
   */
  classTime(startTime, endTime) {
    startTime = new Date(startTime)
    endTime = new Date(endTime)
    return `${startTime.getDate()} ${this.monthName(startTime.getMonth())} ${startTime.getHours()<10?'0':''}${startTime.getHours()}:${startTime.getMinutes()<10?'0':''}${startTime.getMinutes()}-${endTime.getHours()<10?'0':''}${endTime.getHours()}:${endTime.getMinutes()<10?'0':''}${endTime.getMinutes()} (${this.weekName(startTime.getDay())})`
  },
  /*
   * 日历页上课时间
   */
  classTimeInScheduleTable(startTime, endTime) {
    startTime = new Date(startTime)
    endTime = new Date(endTime)
    return `${startTime.getHours()<10?'0':''}${startTime.getHours()}:${startTime.getMinutes()<10?'0':''}${startTime.getMinutes()}-${endTime.getHours()<10?'0':''}${endTime.getHours()}:${endTime.getMinutes()<10?'0':''}${endTime.getMinutes()}`
  },
  /*
  * 多次约课或取消的日期格式 xxxx-xx-xx 例如:2019-09-01
  */
  scheduleTime(utc) {
    let date = new Date(utc);
    let locale = date.toLocaleDateString();
    return locale;
  },
  /**
   * 转成 2019-08-01 16:15 这种格式
   */
  dateClockTime(utc) {
    let date = new Date(utc);
    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 Mi = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let S = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    let tempDate = Y + M + D + H + Mi + S;
    return tempDate
  },

  /**
   * 获取到本地日期对象
   */
  getLocalDate(utc) {
    let date = new Date(utc);
    return date;
  },

  /**
   * 获取两个年份的差值
   */
  getMinusYear(utc){
    let end = new Date();
    let start = this.getLocalDate(utc);
    if(start.getTime() > end.getTime()){
      let temp = start;
      start = end;
      end = temp;
    }
    let yearStart = start.getFullYear()*1;
    let yearEnd = end.getFullYear()*1;
    let year = yearEnd - yearStart;
    let month;
    let monthStart = start.getMonth()*1+1;
    let monthEnd = end.getMonth()*1+1;
    if(monthEnd < monthStart){
      month = monthStart-monthEnd;
    }else{
      month = (monthStart-monthEnd)*-1
    }
    year = year + month / 12;
    return year.toFixed(2);
  },
  formartDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }

    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
        'w+': this._weekdayNameEn2[date.getDay()]
    };

    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
        }
    }
    return fmt;
  },
  padLeftZero(str) {
      return ('00' + str).substr(str.length);
  },
  formartDateFromUTC(utc, fmt){
    let self = this;
    return self.formartDate(self.getLocalDate(utc), fmt);
  },
  addDay(date, days) {
    return new Date(date.getTime() + 1000 * 60 * 60 * 24 * days);
  },
  getTimeZone() {
    let date = (new Date()).toString().substring(28);
    let arr = date.split('(');
    let times = arr[0].replace(' ', '');
    let time = times.substring(0, 3) + times.substring(3)
    // return arr[1].replace(')',' ') + time;
    return time
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值