获取Vuetify时间以12小时格式显示,带有“上午”或“下午”。

huangapple go评论70阅读模式
英文:

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.

Codepen

英文:

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.
获取Vuetify时间以12小时格式显示,带有“上午”或“下午”。

If I pick time in AM, I get no problem but not showing AM.
获取Vuetify时间以12小时格式显示,带有“上午”或“下午”。

If I pick time in PM, this is what I got.
获取Vuetify时间以12小时格式显示,带有“上午”或“下午”。

What I want to do is get the time data with AM and/or PM.

How can I achieve this?
获取Vuetify时间以12小时格式显示,带有“上午”或“下午”。

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' 
}}

Codepen

答案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
    }
  }

更新的 codepen

...如果您真的想要,现在可以将所有部分都压缩到一行,因为各个部分都在工作了

function standardTime() {
    return `${this.time.split(':')[0] % 12 === 0 ? 12 : this.time.split(':')[0] % 12}:${this.time.split(':')[1]} ${this.period.toUpperCase()}`;
}

美好的。希望它不需要任何未来的维护 获取Vuetify时间以12小时格式显示,带有“上午”或“下午”。

英文:

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

&lt;h3&gt;
    12hrs time: 
    {{ standardTime() }}
&lt;/h3&gt;
&lt;input type=&quot;time&quot; v-model=&quot;time&quot; /&gt;
&lt;v-time-picker v-model=&quot;time&quot; @update:period=&quot;changePeriod&quot;&gt;&lt;/v-time-picker&gt;
data() {
    return {
      time: &#39;&#39;,
      period: &#39;am&#39;
    }
  },
  methods: {
    standardTime() {
      const militaryHours = this.time.split(&#39;:&#39;)[0]
      const minutes = this.time.split(&#39;:&#39;)[1]
      const standardHours = militaryHours % 12 === 0 ? 12 : militaryHours % 12
      return `${standardHours}:${minutes} ${this.period.toUpperCase()}`;
    },
    changePeriod(period) {
      this.period = period
    }
  }

updated codepen

...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(&#39;:&#39;)[0] % 12 === 0 ? 12 : this.time.split(&#39;:&#39;)[0] % 12}:${this.time.split(&#39;:&#39;)[1]} ${this.period.toUpperCase()}`;
}

Lovely. Hopefully it won't require any future maintenance 获取Vuetify时间以12小时格式显示,带有“上午”或“下午”。

答案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: &#39;#app&#39;,
  vuetify: new Vuetify(),
  data() {
    return {
      time: &#39;&#39;,
    }
  }
})

<!-- language: lang-html -->

&lt;link href=&quot;https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.1.12/vuetify.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/vuetify/2.1.12/vuetify.min.js&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;app&quot;&gt;
    &lt;v-app&gt;
        &lt;v-card id=&quot;table-card&quot;&gt;
          &lt;div&gt;
            &lt;h3&gt;
                24hrs time: 
                {{ time }}
            &lt;/h3&gt;

            &lt;h3&gt;
                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(&quot;:&quot;))
                      .toLocaleString(undefined, { timeStyle: &quot;short&quot; })
                }}
            &lt;/h3&gt;
            &lt;input type=&quot;time&quot; v-model=&quot;time&quot; /&gt;
            &lt;v-time-picker v-model=&quot;time&quot;&gt;&lt;/v-time-picker&gt;
        &lt;/div&gt;
        &lt;/v-card&gt;
    &lt;/v-app&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 07:53:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定