Skip to content

Commit fb044b7

Browse files
committed
Add date and time localization questions
1 parent 81173e0 commit fb044b7

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ List of 300 VueJS Interview Questions
216216
|207| [#What are the types of formatting?](#what-are-the-types-of-formatting)|
217217
|208| [What is custom formatting?](#what-is-custom-formatting)|
218218
|209| [How do you handle Pluralization?](#how-do-you-handle-pluralization)|
219+
|210| [How to implement DateTime localization?](#how-to-implement-date-time-localization)|
219220

220221
1. ### What is VueJS?
221222
**Vue.js** is an open-source, progressive Javascript framework for building user interfaces that aim to be incrementally adoptable. The core library of VueJS is focused on the `view layer` only, and is easy to pick up and integrate with other libraries or existing projects.
@@ -3722,5 +3723,57 @@ List of 300 VueJS Interview Questions
37223723
<p>one friend</p>
37233724
<p>10 friends</p>
37243725
```
3726+
210. ### How to implement DateTime localization?
3727+
You can localize the datetime with definition formats(e.g. short, long, etc).
3728+
Lets follow below steps to localize date and time
3729+
1. For example, you can define definition formats for English and Jappan locale as below
3730+
```javascript
3731+
const dateTimeFormats = {
3732+
'en-US': {
3733+
short: {
3734+
year: 'numeric', month: 'short', day: 'numeric'
3735+
},
3736+
long: {
3737+
year: 'numeric', month: 'short', day: 'numeric',
3738+
weekday: 'short', hour: 'numeric', minute: 'numeric'
3739+
}
3740+
},
3741+
'ja-JP': {
3742+
short: {
3743+
year: 'numeric', month: 'short', day: 'numeric'
3744+
},
3745+
long: {
3746+
year: 'numeric', month: 'short', day: 'numeric',
3747+
weekday: 'short', hour: 'numeric', minute: 'numeric', hour12: true
3748+
}
3749+
}
3750+
}
3751+
```
3752+
2. After that You need to specify the dateTimeFormats option of VueI18n constructor
3753+
```javascript
3754+
const i18n = new VueI18n({
3755+
dateTimeFormats
3756+
})
3757+
3758+
new Vue({
3759+
i18n
3760+
}).$mount('#app')
3761+
```
3762+
3. And then add them to the template
3763+
```javascript
3764+
<div id="app">
3765+
<p>{{ $d(new Date(), 'short') }}</p>
3766+
<p>{{ $d(new Date(), 'long', 'ja-JP') }}</p>
3767+
</div>
3768+
```
3769+
4. Finally it outputs the result
3770+
```javascript
3771+
<div id="app">
3772+
<p>May 20, 2019</p>
3773+
<p>2019520</p>
3774+
</div>
3775+
```
3776+
3777+
37253778
37263779

0 commit comments

Comments
 (0)