Skip to content

Commit 6b16656

Browse files
authored
Merge pull request #2 from Shunpoco/feature/initialize
fix: add dateToStr
2 parents 5e15659 + 08592fa commit 6b16656

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/App.vue

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<div class="container">
1010
<h3>Example2: Treat a value as a Date Object</h3>
1111
<input type="date"
12-
v-bind:value="date2"
12+
v-bind:value="dateToStr(date2)"
1313
v-on:input="date2 = strToDate($event.target.value)"
1414
/>
1515
<p>{{ date2 }}</p>
@@ -36,8 +36,16 @@ export default class App extends Vue {
3636
private date2: Date = new Date();
3737
private date3: Date = new Date();
3838
39-
strToDate(date: string): Date {
40-
return new Date(date);
39+
strToDate(str: string): Date {
40+
return new Date(str);
41+
}
42+
43+
dateToStr(date: Date): string {
44+
const year = date.getFullYear();
45+
const month = date.getMonth() + 1 < 9 ? `0${date.getMonth() + 1}` : `${date.getMonth() + 1}`;
46+
const day = date.getDate() < 9 ? `0${date.getDate()}` : `${date.getDate()}`;
47+
const strDate = `${year}-${month}-${day}`;
48+
return strDate;
4149
}
4250
}
4351
</script>

src/components/MyDate.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div>
33
<input type="date"
4-
v-bind="date"
4+
v-bind:value="dateToStr(date)"
55
v-on:input="date = strToDate($event.target.value)"
66
/>
77
<p>{{ date }}</p>
@@ -19,6 +19,14 @@ class MyDate extends Vue {
1919
private strToDate(str: string): Date {
2020
return new Date(str);
2121
}
22+
23+
private dateToStr(date: Date): string {
24+
const year = date.getFullYear();
25+
const month = date.getMonth() + 1 < 9 ? `0${date.getMonth() + 1}` : `${date.getMonth() + 1}`;
26+
const day = date.getDate() < 9 ? `0${date.getDate()}` : `${date.getDate()}`;
27+
const strDate = `${year}-${month}-${day}`;
28+
return strDate;
29+
}
2230
}
2331
2432
export default MyDate;

0 commit comments

Comments
 (0)