@@ -5,7 +5,7 @@ import {CommonUtils} from "../core/utils/common-utils";
55 * 时间粒度值
66 */
77export enum TimeGr {
8- second , minute , hour , date , week , month
8+ second , minute , hour , date , week , month , time , time_hour_minute , time_minute_second
99}
1010
1111/**
@@ -37,14 +37,14 @@ export enum TimeWeekDayStart {
3737 * `TimeService`提供了许多有用的时间换算、格式化工具,当你有需要对时间进行运算时,可以参考它的api说。
3838 */
3939export enum TimeUnit {
40- s , m , h , d , w , M , y
40+ s , m , h , d , w , M , y
4141}
4242
4343/**
4444 * 常用时间格式,`TimeService.format`可以支持任何格式,我们做这个枚举只是他们太常用了,使用这个枚举+IDE提示,你可以少敲很多次键盘。
4545 */
4646export enum TimeFormatters {
47- yyyy_mm_dd_hh_mm_ss , yyyy_mm_dd_hh_mm , yyyy_mm_dd_hh , yyyy_mm_dd , yyyy_mm
47+ yyyy_mm_dd_hh_mm_ss , yyyy_mm_dd_hh_mm , yyyy_mm_dd_hh , yyyy_mm_dd , yyyy_mm , hh_mm_ss , hh_mm , mm_ss
4848}
4949
5050/**
@@ -64,26 +64,37 @@ export class TimeService {
6464 * @param {TimeGr } gr 目标粒度
6565 * @return {string } 符合粒度格式的时刻
6666 */
67- public static convertValue ( value : WeekTime , gr :TimeGr ) : string {
67+ public static convertValue ( value : WeekTime , gr : TimeGr ) : string {
6868 value = TimeService . _handleWeekValue ( value ) ;
6969 value = TimeService . getFormatDate ( < Time > value , gr ) ;
7070 return < string > value ;
7171 }
7272
73- private static _handleWeekValue ( newValue :WeekTime ) : Time {
73+ private static _handleWeekValue ( newValue : WeekTime ) : Time {
7474 if ( newValue && typeof newValue [ "week" ] === 'number' ) {
7575 return TimeService . getDateFromYearAndWeek ( newValue [ "year" ] , newValue [ "week" ] )
7676 }
7777 return < Time > newValue ;
7878 }
7979
80- private static _timeFormatterConvert ( formatter : TimeFormatters ) :string {
81- switch ( formatter ) {
82- case TimeFormatters . yyyy_mm_dd_hh_mm_ss : return "YYYY-MM-DD HH:mm:ss" ;
83- case TimeFormatters . yyyy_mm_dd_hh_mm : return "YYYY-MM-DD HH:mm" ;
84- case TimeFormatters . yyyy_mm_dd_hh : return "YYYY-MM-DD HH" ;
85- case TimeFormatters . yyyy_mm_dd : return "YYYY-MM-DD" ;
86- case TimeFormatters . yyyy_mm : return "YYYY-MM" ;
80+ private static _timeFormatterConvert ( formatter : TimeFormatters ) : string {
81+ switch ( formatter ) {
82+ case TimeFormatters . yyyy_mm_dd_hh_mm_ss :
83+ return "YYYY-MM-DD HH:mm:ss" ;
84+ case TimeFormatters . yyyy_mm_dd_hh_mm :
85+ return "YYYY-MM-DD HH:mm" ;
86+ case TimeFormatters . yyyy_mm_dd_hh :
87+ return "YYYY-MM-DD HH" ;
88+ case TimeFormatters . yyyy_mm_dd :
89+ return "YYYY-MM-DD" ;
90+ case TimeFormatters . yyyy_mm :
91+ return "YYYY-MM" ;
92+ case TimeFormatters . hh_mm_ss :
93+ return "HH:mm:ss" ;
94+ case TimeFormatters . hh_mm :
95+ return "HH:mm" ;
96+ case TimeFormatters . mm_ss :
97+ return "mm:ss" ;
8798 }
8899 }
89100
@@ -93,7 +104,10 @@ export class TimeService {
93104 [ TimeGr . hour , TimeService . _timeFormatterConvert ( TimeFormatters . yyyy_mm_dd_hh ) ] ,
94105 [ TimeGr . date , TimeService . _timeFormatterConvert ( TimeFormatters . yyyy_mm_dd ) ] ,
95106 [ TimeGr . week , TimeService . _timeFormatterConvert ( TimeFormatters . yyyy_mm_dd ) ] ,
96- [ TimeGr . month , TimeService . _timeFormatterConvert ( TimeFormatters . yyyy_mm ) ]
107+ [ TimeGr . month , TimeService . _timeFormatterConvert ( TimeFormatters . yyyy_mm ) ] ,
108+ [ TimeGr . time , TimeService . _timeFormatterConvert ( TimeFormatters . hh_mm_ss ) ] ,
109+ [ TimeGr . time_hour_minute , TimeService . _timeFormatterConvert ( TimeFormatters . hh_mm ) ] ,
110+ [ TimeGr . time_minute_second , TimeService . _timeFormatterConvert ( TimeFormatters . mm_ss ) ] ,
97111 ] ) ;
98112
99113 /**
@@ -102,8 +116,8 @@ export class TimeService {
102116 * @param {TimeUnit } unit 时间单位枚举值
103117 * @return {string } 返回对应的字符串
104118 */
105- public static timeUnitConvert ( unit : TimeUnit ) :string {
106- return TimeUnit [ unit ] ;
119+ public static timeUnitConvert ( unit : TimeUnit ) : string {
120+ return TimeUnit [ unit ] ;
107121 }
108122
109123 private static _timeUnitMap = new Map ( [
@@ -119,7 +133,7 @@ export class TimeService {
119133 private static _initMoment ( ) {
120134 try {
121135 moment . suppressDeprecationWarnings = 1 ;
122- } catch ( e ) {
136+ } catch ( e ) {
123137 }
124138 }
125139
@@ -136,7 +150,7 @@ export class TimeService {
136150 * @param {Time } time 给定的时刻值
137151 * @returns {boolean } 如果给定的值非字符串,则必然返回false。
138152 */
139- public static isMacro ( time :Time ) : boolean {
153+ public static isMacro ( time : Time ) : boolean {
140154 if ( typeof time === 'string' ) {
141155 return ! ! time . match ( / ^ \s * ( n o w | t o d a y | y e s t o d a y | t o m o r r o w ) \s * ( [ + - ] \s * \d + \s * \w + ) ? \s * $ / i) ;
142156 }
@@ -148,7 +162,7 @@ export class TimeService {
148162 * @param timeMacro
149163 * @returns {any }
150164 */
151- private static _convertBasicMacro ( timeMacro :string ) : Date | string {
165+ private static _convertBasicMacro ( timeMacro : string ) : Date | string {
152166 let date ;
153167 switch ( timeMacro ) {
154168 case 'now' :
@@ -168,7 +182,7 @@ export class TimeService {
168182 * @param {string|number } num 数量,为负数即为减法
169183 * @param {TimeUnit } unit 单位
170184 */
171- public static addDate ( date :Time , num :string | number , unit :TimeUnit ) : Moment {
185+ public static addDate ( date : Time , num : string | number , unit : TimeUnit ) : Moment {
172186 return moment ( date ) . add ( num , TimeService . _timeUnitMap . get ( unit ) ) ;
173187 }
174188
@@ -190,7 +204,7 @@ export class TimeService {
190204 * @param {string | TimeFormatters } formatter
191205 */
192206 public static format ( date : Time , formatter : string | TimeFormatters ) : string {
193- if ( typeof formatter === "number" ) formatter = TimeService . _timeFormatterConvert ( formatter ) ;
207+ if ( typeof formatter === "number" ) formatter = TimeService . _timeFormatterConvert ( formatter ) ;
194208 return moment ( date ) . format ( formatter ) ;
195209 }
196210
@@ -200,7 +214,7 @@ export class TimeService {
200214 * @param {Time } date
201215 * @param {TimeGr } gr
202216 */
203- public static formatWithGr ( date :Time , gr :TimeGr ) : string {
217+ public static formatWithGr ( date : Time , gr : TimeGr ) : string {
204218 let format = TimeService . getFormatter ( gr ) ;
205219 return moment ( date ) . format ( format ) ;
206220 }
@@ -230,7 +244,7 @@ export class TimeService {
230244 * @param date
231245 *
232246 */
233- public static getWeekYear ( date :Time ) : number {
247+ public static getWeekYear ( date : Time ) : number {
234248 return moment ( date ) . weekYear ( ) ;
235249 }
236250
@@ -240,7 +254,7 @@ export class TimeService {
240254 * @param {Time } date
241255 * @return {number }
242256 */
243- public static getWeekOfYear ( date :Time ) : number {
257+ public static getWeekOfYear ( date : Time ) : number {
244258 return moment ( date ) . week ( ) ;
245259 }
246260
@@ -250,7 +264,7 @@ export class TimeService {
250264 * @param {Time } date
251265 * @return {number }
252266 */
253- public static getYear ( date :Time ) : number {
267+ public static getYear ( date : Time ) : number {
254268 return moment ( date ) . year ( ) ;
255269 }
256270
@@ -260,7 +274,7 @@ export class TimeService {
260274 * @param {Time } date
261275 * @return {number }
262276 */
263- public static getMonth ( date :Time ) : number {
277+ public static getMonth ( date : Time ) : number {
264278 return moment ( date ) . month ( ) + 1 ;
265279 }
266280
@@ -270,15 +284,15 @@ export class TimeService {
270284 * @param {Time } date
271285 * @return {number }
272286 */
273- public static getDay ( date :Time ) : number {
287+ public static getDay ( date : Time ) : number {
274288 return moment ( date ) . date ( ) ;
275289 }
276290
277291 public static getDateFromYearAndWeek ( year : number , week : number ) : Date {
278292 return moment ( ) . weekYear ( year ) . week ( week )
279293 }
280294
281- public static getDate ( str :Time , gr : TimeGr ) : Moment {
295+ public static getDate ( str : Time , gr : TimeGr ) : Moment {
282296 return moment ( str , TimeService . getFormatter ( gr ) ) ;
283297 }
284298
0 commit comments