Projekt

Obecné

Profil

Stáhnout (1.11 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Injectable} from '@angular/core';
2

    
3
@Injectable({
4
  providedIn: 'root'
5
})
6
export class DateToolsService {
7
  constructor() { }
8

    
9
  /**
10
   * Returns start of passed month
11
   * @param date date
12
   */
13
  toStartOfMonth(date: Date): Date {
14
    return new Date(
15
      date.getFullYear(),
16
      date.getMonth(),
17
      1,
18
      0,
19
      0,
20
      0,
21
      0
22
    );
23
  }
24

    
25
  /**
26
   * Returns end of passed month
27
   * @param date date
28
   */
29
  toEndOfMonth(date: Date): Date {
30
    return new Date(
31
      date.getFullYear(),
32
      date.getMonth() + 1,
33
      0,
34
      23,
35
      59,
36
      59,
37
      999
38
    );
39
  }
40

    
41
  /**
42
   * Creates Date from date and time
43
   *
44
   * @param date date in format YYYY/mm/dd
45
   * @param time time
46
   */
47
  toDate(date: string, time: string): Date {
48
    const splittedDate = date.split('/');
49

    
50
    const result = new Date(
51
      Number(splittedDate[0]),
52
      Number(splittedDate[1]) - 1,
53
      Number(splittedDate[2])
54
    );
55

    
56
    if (time) {
57
      const splittedTime = time.split(':');
58
      result.setHours(Number(splittedTime[0]));
59
      result.setMinutes(Number(splittedTime[1]));
60
    }
61

    
62
    return result;
63
  }
64
}
(2-2/4)