zoukankan      html  css  js  c++  java
  • Angular6 项目开发常用时间组件服务

    一、利用Angular 命令行工具生成一个服务。

      详情见:《Angular环境搭建》,服务代码如下:

      1 import { Injectable } from '@angular/core';
      2 
      3 @Injectable({
      4   providedIn: 'root'
      5 })
      6 export class TimeutilProvider {
      7 
      8   private today: Date;
      9   private year: String;
     10   private mouth: String;
     11   private date: String;
     12   private hours: String;
     13   private minutes: String;
     14   private seconds: String;
     15   private milliseconds: String;
     16   private week:number;
     17   private weekday:Array<string> =["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
     18   constructor () {
     19     this.today = new Date();
     20     this.year = this.today.getFullYear().toString();
     21     this.mouth = (this.today.getMonth() + 1).toString();
     22     this.date = this.today.getDate().toString();
     23     this.hours = this.today.getHours().toString();
     24     this.minutes = this.today.getMinutes().toString();
     25     this.seconds = this.today.getSeconds().toString();
     26     this.milliseconds = this.today.getMilliseconds().toString();
     27 
     28     this.week = this.today.getDay();
     29 
     30     if (Number(this.mouth) < 10) {
     31       this.mouth = '0' + this.mouth;
     32     } else {
     33     }
     34     if (Number(this.date) < 10) {
     35       this.date = '0' + this.date;
     36     } else {
     37     }
     38     if (Number(this.hours) < 10) {
     39       this.hours = '0' + this.hours;
     40     } else {
     41     }
     42     if (Number(this.minutes) < 10) {
     43       this.minutes = '0' + this.minutes;
     44     } else {
     45     }
     46     if (Number(this.seconds) < 10) {
     47       this.seconds = '0' + this.seconds;
     48     }
     49     if (10 > Number(this.milliseconds)) {
     50       this.milliseconds = '00' + this.milliseconds;
     51     } else if ((10 <= Number(this.milliseconds)) && (100 > Number(this.milliseconds))) {
     52       this.milliseconds = '0' + this.milliseconds;
     53     } else {
     54       // dothing
     55     }
     56   }
     57 
     58   //返回当前星期
     59   currentWeek(){
     60     return this.weekday[this.week].toString();
     61     // this.week.toString();
     62   }
     63 
     64   //20190306115830234格式
     65   currentTime() {
     66     return this.year + '' + this.mouth + '' + this.date + '' + this.hours + '' + this.minutes + '' + this.seconds + '' + this.milliseconds;
     67   }
     68 
     69   //一般用于生成唯一标识,例如uuid
     70   currentTime_id() {
     71     // 三位随机数以免重复
     72     const random = (Math.random() * 100000000000000000).toString().substr(0, 3);
     73     
     74     return this.year + '' + this.mouth + '' + this.date + ''
     75     + this.hours + '' + this.minutes + '' + this.seconds + '' + this.milliseconds + random;
     76   }
     77 
     78   //2019-03-06 11:59格式
     79   currentTime_datestyle() {
     80     return this.year + '-' + this.mouth + '-' + this.date + ' ' + this.hours + ':' + this.minutes + '';
     81   }
     82 
     83   //2019-03-06 11:59:30格式
     84   currentTime_datesecondsstyle() {
     85     return this.year + '-' + this.mouth + '-' + this.date + ' ' + this.hours + ':' + this.minutes + ':' + this.seconds;
     86   }
     87   
     88   
     89   // currentTime_date(){
     90   //   return this.year + '-' + this.mouth + '-' + this.date;
     91   // }
     92 
     93   // 返回当前时间"10:25:34"格式
     94   currentTime_time(){
     95     let now=new Date();
     96     let year  = (now.getFullYear()).toString();
     97     let month = (now.getMonth()+1).toString();
     98     let date  = (now.getDate()).toString();
     99     let hour  = (now.getHours()).toString();
    100     let minute= (now.getMinutes()).toString();
    101     let second= (now.getSeconds()).toString();
    102 
    103     if (Number(month) < 10) {
    104           month = '0' + month;
    105     } else {
    106     }
    107     if (Number(date) < 10) {
    108           date = '0' + date;
    109     } else {
    110     }
    111     if (Number(hour) < 10) {
    112           hour = '0' + hour;
    113     } else {
    114     }
    115     if (Number(minute) < 10) {
    116           minute = '0' + minute;
    117     } else {
    118     }
    119     if (Number(second) < 10) {
    120           second = '0' + second;
    121     }
    122 
    123     return hour + ':' + minute + ':' + second;
    124   }
    125 
    126   
    127 
    128   //将当前时间转化为消息类型时间,2019.03.06 11:56:30格式
    129   convertToDate_msgTimestamp(nows){
    130     let now=new Date(nows);
    131     let year  = (now.getFullYear()).toString();
    132     let month = (now.getMonth()+1).toString();
    133     let date  = (now.getDate()).toString();
    134     let hour  = (now.getHours()).toString();
    135     let minute= (now.getMinutes()).toString();
    136     let second= (now.getSeconds()).toString();
    137 
    138     if (Number(month) < 10) {
    139           month = '0' + month;
    140     } else {
    141     }
    142     if (Number(date) < 10) {
    143           date = '0' + date;
    144     } else {
    145     }
    146     if (Number(hour) < 10) {
    147           hour = '0' + hour;
    148     } else {
    149     }
    150     if (Number(minute) < 10) {
    151           minute = '0' + minute;
    152     } else {
    153     }
    154     if (Number(second) < 10) {
    155           second = '0' + second;
    156     }
    157       return year+"."+month+"."+date+" "+hour+":"+minute+":"+second; 
    158   }
    159 
    160   //2019-03-06 11:56:30格式
    161   convertToDate_timestamp(nows){
    162     var now=new Date(nows);
    163     var year  = (now.getFullYear()).toString();
    164     var month = (now.getMonth()+1).toString();
    165     var date  = (now.getDate()).toString();
    166     var hour  = (now.getHours()).toString();
    167     var minute= (now.getMinutes()).toString();
    168     var second= (now.getSeconds()).toString();
    169 
    170     if (Number(month) < 10) {
    171           month = '0' + month;
    172     } else {
    173     }
    174     if (Number(date) < 10) {
    175           date = '0' + date;
    176     } else {
    177     }
    178     if (Number(hour) < 10) {
    179           hour = '0' + hour;
    180     } else {
    181     }
    182     if (Number(minute) < 10) {
    183           minute = '0' + minute;
    184     } else {
    185     }
    186     if (Number(second) < 10) {
    187           second = '0' + second;
    188     }
    189       return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second; 
    190   }
    191 
    192 
    193   //将消息时间格式转化为时间戳格式
    194   convertTotimestamp(date:string){
    195     return date.replace(".","-");
    196   }
    197 
    198 
    199   //将消息时间格式转化为Date类型
    200   convertMsgtimeToDate(date:string){
    201     //date = "2018.11.01 10:00:00";
    202     date.replace(".","-");
    203     return new Date(date)
    204   }
    205 
    206   //将时间戳格式日期转化为Date类型
    207   convertTimestampToDate(date:string){    
    208     return new Date(date)
    209   }
    210 
    211   //获取当前时间前一天时间
    212   getYestardayTime(){
    213    return new Date(new Date().getTime() - 24*60*60*1000); 
    214   }
    215 
    216   //返回当前日期“2018-02-03”格式
    217   currentTime_daystyle() {
    218       return this.year + "-" + this.mouth + "-" + this.date + " ";
    219   }
    220 
    221   //返回当前时间前一天时间“2018.02.03”格式
    222   currentPreviousTime_daystyle() {
    223     let previousDate = new Date(new Date().getTime() - 24*60*60*1000); 
    224     var year  = (previousDate.getFullYear()).toString();
    225     var month = (previousDate.getMonth()+1).toString();
    226     var date  = (previousDate.getDate()).toString();
    227     if (Number(month) < 10) {
    228       month = '0' + month;
    229     } else {
    230     }
    231     if (Number(date) < 10) {
    232           date = '0' + date;
    233     } else {
    234     }
    235 
    236     return year + "." + month + "." + date + " ";
    237   }
    238 }

    二、时间服务的使用

      1.在要是用的组建构造函数中依赖注入,如下图: 

    1 constructor(private timeutil : TimeutilProvider) {
    2      //调用方法
    3      console.log("id:",this.timeutil.currentTime_id(););
    4 }    
  • 相关阅读:
    走进Android系统
    最后的一像素。
    rem介绍
    软件安装相关问题。
    iscroll
    程序员常用词语发音
    一个程序员眼中的好UI
    myeclipse性能优化
    注入配置数据
    java IO
  • 原文地址:https://www.cnblogs.com/mycnblogs-guoguo/p/10482754.html
Copyright © 2011-2022 走看看