我是靠谱客的博主 淡定裙子,最近开发中收集的这篇文章主要介绍获取当前日期,时间,周数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

/**
* description: 获取当前日期,时间,周数
* create_time: 2020-6-19
* function: getDate,getTime,getWeek
*/

class DateTime {
    // 构造方法
    constructor() {
      // 列举Week
      this.weekday = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
      // 获取当前时间
      this.Date = new Date();
      // 获取当前年
      this.year = this.Date.getFullYear();
      // 获取当前月
      this.month = this.Date.getMonth() + 1;
      // 获取当前日
      this.date = this.Date.getDate();
      // 获取当前星期几
      this.day = this.Date.getDay();
      // 获取小时
      this.hour = this.Date.getHours();
      // 获取分钟
      this.minute = this.Date.getMinutes();
      // 获取秒
      this.second = this.Date.getSeconds();
      // 自动补零
      this.month = (this.month < 10) ? '0' + this.month : this.month = this.month;
      this.date = (this.date < 10) ? '0' + this.date : this.date = this.date;
      this.minute = (this.minute < 10) ? '0' + this.minute : this.minute = this.minute;
      this.second = (this.second < 10) ? '0' + this.second : this.second = this.second;
    }
    /**
    * 取当前日期
    * @returns {string}
    */
    getDate() {
      return this.year + '-' + this.month + '-' + this.date;
    }
    /**
    * 获取当前时间
    * @returns {string}
    */
    getTime() {
      return this.hour + ':' + this.minute + ':' + this.second;
    }
    /**
    * 获取当前星期几
    *@returns {string}
    */
    getWeek() {
      return this.weekday[this.day]
    }
  }
  export { DateTime }

最后

以上就是淡定裙子为你收集整理的获取当前日期,时间,周数的全部内容,希望文章能够帮你解决获取当前日期,时间,周数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部