英文:
Vuetify time get value in 12hrs format with "A.M." or "P.M."
问题
I want to do an "time picker" in vuetify and get the value of time in 12hrs format with "A.M." or "P.M."
I attempt to get the time in 12hrs but there's an error.
First load. I get :undefined value.
If I pick time in AM, I get no problem but not showing AM.
If I pick time in PM, this is what I got.
What I want to do is get the time data with AM and/or PM.
How can I achieve this?
I attempt this kind of condition, but It's not getting the "A.M" / "P.M" value.
英文:
I want to do an time picker
in vuetify and get the value of time in 12hrs format with " A.M. " or " P.M. "
I attempt to get the time in 12hrs but there's an error.
First load. I get :undefined value.
If I pick time in AM, I get no problem but not showing AM.
If I pick time in PM, this is what I got.
What I want to do is get the time data with AM and/or PM.
I attempt this kind of condition, but It's not getting the "A.M" / "P.M" value.
{{
(time.split(':')[0] >= 13 ? time.split(':')[0] % 12 : time.split(':')[0])
+ ':' + time.split(':')[1], time.split(':')[0] >= 13 ? 'PM' : 'AM'
}}
答案1
得分: 2
以下是您要翻译的内容:
个人而言,尝试在一行上完成所有操作会导致像这样的问题。很难使其正常工作,更难以阅读/理解。我更喜欢将其拆分为一个可以在多个步骤中执行和理解的方法。使用 v-timepicker 更新:period 方法来跟踪上午/下午也有所帮助
<h3>
12小时制时间:
{{ standardTime() }}
</h3>
<input type="time" v-model="time" />
<v-time-picker v-model="time" @update:period="changePeriod"></v-time-picker>
data() {
return {
time: '',
period: '上午'
}
},
methods: {
standardTime() {
const militaryHours = this.time.split(':')[0]
const minutes = this.time.split(':')[1]
const standardHours = militaryHours % 12 === 0 ? 12 : militaryHours % 12
return `${standardHours}:${minutes} ${this.period.toUpperCase()}`;
},
changePeriod(period) {
this.period = period
}
}
...如果您真的想要,现在可以将所有部分都压缩到一行,因为各个部分都在工作了
function standardTime() {
return `${this.time.split(':')[0] % 12 === 0 ? 12 : this.time.split(':')[0] % 12}:${this.time.split(':')[1]} ${this.period.toUpperCase()}`;
}
美好的。希望它不需要任何未来的维护
英文:
Personally, trying to do everything on one line leads to issues like this for me. It's hard to get working right, and it's even harder to read/understand. I prefer to break it out into a method where the process can be performed and understood over multiple steps. Using the v-timepicker update:period method to keep track of AM/PM also helps
<h3>
12hrs time:
{{ standardTime() }}
</h3>
<input type="time" v-model="time" />
<v-time-picker v-model="time" @update:period="changePeriod"></v-time-picker>
data() {
return {
time: '',
period: 'am'
}
},
methods: {
standardTime() {
const militaryHours = this.time.split(':')[0]
const minutes = this.time.split(':')[1]
const standardHours = militaryHours % 12 === 0 ? 12 : militaryHours % 12
return `${standardHours}:${minutes} ${this.period.toUpperCase()}`;
},
changePeriod(period) {
this.period = period
}
}
...and if you really want, you can condense it all into one line now that the individual parts are working
function standardTime() {
return `${this.time.split(':')[0] % 12 === 0 ? 12 : this.time.split(':')[0] % 12}:${this.time.split(':')[1]} ${this.period.toUpperCase()}`;
}
Lovely. Hopefully it won't require any future maintenance
答案2
得分: 0
我建议您利用原生的Intl.DateTimeFormat
API。它允许您配置输出字符串的样式,并适应用户的区域设置。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
time: '',
}
}
})
<!-- language: lang-html -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.1.12/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.1.12/vuetify.min.js"></script>
<div id="app">
<v-app>
<v-card id="table-card">
<div>
<h3>
24小时制时间:
{{ time }}
</h3>
<h3>
12小时制时间:
{{
// 年份 = 0,月份 = 0,日期 = 0。时和分设置为时间选择器中的时和分
new Date(0, 0, 0, ...time.split(":"))
.toLocaleString(undefined, { timeStyle: "short" })
}}
</h3>
<input type="time" v-model="time" />
<v-time-picker v-model="time"></v-time-picker>
</div>
</v-card>
</v-app>
</div>
<!-- end snippet -->
英文:
I suggest that you take advantage of the native Intl.DateTimeFormat
API. It allows you to configure the style of the string outputted and adapts to the user's locale.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
time: '',
}
}
})
<!-- language: lang-html -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.1.12/vuetify.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.1.12/vuetify.min.js"></script>
<div id="app">
<v-app>
<v-card id="table-card">
<div>
<h3>
24hrs time:
{{ time }}
</h3>
<h3>
12hrs time:
{{
// year = 0, month = 0, day = 0. hour and minute are set to the hour and minute from the time picker
new Date(0, 0, 0, ...time.split(":"))
.toLocaleString(undefined, { timeStyle: "short" })
}}
</h3>
<input type="time" v-model="time" />
<v-time-picker v-model="time"></v-time-picker>
</div>
</v-card>
</v-app>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论