summaryrefslogtreecommitdiff
path: root/std/datetime/README.md
blob: 62e15df2e3ee2ec35eb2a789a64bc00d2675420e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# datetime

Simple helper to help parse date strings into `Date`, with additional functions.

## Usage

The following symbols are supported:

- `yyyy` - numeric year
- `yy` - 2-digit year
- `M` - numeric month
- `MM` - 2-digit month
- `d` - numeric day
- `dd` - 2-digit day

- `h` - numeric hour
- `hh` - 2-digit hour
- `m` - numeric minute
- `mm` - 2-digit minute
- `s` - numeric second
- `ss` - 2-digit second
- `S` - 1-digit fractionalSecond
- `SS` - 2-digit fractionalSecond
- `SSS` - 3-digit fractionalSecond

- `a` - dayPeriod, either `AM` or `PM`

- `'foo'` - quoted literal
- `./-` - unquoted literal

### parse

Takes an input `string` and a `formatString` to parse to a `date`.

```ts
import { parse } from 'https://deno.land/std/datetime/mod.ts'

parse("20-01-2019", "dd-MM-yyyy") // output : new Date(2019, 0, 20)
parse("2019-01-20", "yyyy-MM-dd") // output : new Date(2019, 0, 20)
parse("2019-01-20", "dd.MM.yyyy") // output : new Date(2019, 0, 20)
parse("01-20-2019 16:34", "MM-dd-yyyy hh:mm") // output : new Date(2019, 0, 20, 16, 34)
parse("01-20-2019 04:34 PM", "MM-dd-yyyy hh:mm a") // output : new Date(2019, 0, 20, 16, 34)
parse("16:34 01-20-2019", "hh:mm MM-dd-yyyy") // output : new Date(2019, 0, 20, 16, 34)
parse("01-20-2019 16:34:23.123", "MM-dd-yyyy hh:mm:ss.SSS") // output : new Date(2019, 0, 20, 16, 34, 23, 123)
...
```

### format

Takes an input `date` and a `formatString` to format to a `string`.

```ts
import { format } from 'https://deno.land/std/datetime/mod.ts'

format(new Date(2019, 0, 20), "dd-MM-yyyy") // output : "20-01-2019"
format(new Date(2019, 0, 20), "yyyy-MM-dd") // output : "2019-01-20"
format(new Date(2019, 0, 20), "dd.MM.yyyy") // output : "2019-01-20"
format(new Date(2019, 0, 20, 16, 34), "MM-dd-yyyy hh:mm") // output : "01-20-2019 16:34"
format(new Date(2019, 0, 20, 16, 34), "MM-dd-yyyy hh:mm a") // output : "01-20-2019 04:34 PM"
format(new Date(2019, 0, 20, 16, 34), "hh:mm MM-dd-yyyy") // output : "16:34 01-20-2019"
format(new Date(2019, 0, 20, 16, 34, 23, 123), "MM-dd-yyyy hh:mm:ss.SSS") // output : "01-20-2019 16:34:23.123"
format(new Date(2019, 0, 20), "'today:' yyyy-MM-dd") // output : "today: 2019-01-20"

...
```

### dayOfYear

Returns the number of the day in the year.

```ts
import { dayOfYear } from "https://deno.land/std/datetime/mod.ts";

dayOfYear(new Date("2019-03-11T03:24:00")); // output: 70
```

### weekOfYear

Returns the ISO week number of the provided date (1-53)

```ts
import { weekOfYear } from "https://deno.land/std/datetime/mod.ts";

weekOfYear(new Date("2020-12-28T03:24:00")); // Returns 53
```