Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
skanfd committed Feb 9, 2021
1 parent a438dd9 commit 335625d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Deno",
"type": "pwa-node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": [
"run",
"--inspect-brk",
"-A",
"${file}"
],
"attachSimplePort": 9229
}
]
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"deno.codeLens.references": true,
"deno.codeLens.referencesAllFunctions": true,
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
# deno-local8601

Generate a ISO8601 string with local timezone, like "2021-02-09T12:20:04+08:00".

## function local8601(date?: Date): string

```typescript
import local8601 from "./mod.ts";

console.log(local8601());

console.log(local8601(new Date()));
```

```
2021-02-09T12:20:04+08:00
2021-02-09T12:20:04+08:00
```
19 changes: 19 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default function local8601(date?: Date): string {
const d = date || new Date();
const value = d.valueOf();
const offset = -d.getTimezoneOffset();

let localstr = new Date(value + offset * 60 * 1000)
.toISOString().slice(0, -5);
localstr += offset > 0 ? "+" : "-";

let hours = Math.floor(offset / 60).toString();
hours = hours.length === 2 ? hours : "0" + hours;

let minutes = (offset % 60).toString();
minutes = minutes.length === 2 ? minutes : "0" + minutes;

localstr += (hours + ":" + minutes);

return localstr;
}

0 comments on commit 335625d

Please sign in to comment.