如何使用时刻js从日期时间字符串获取pm [英] How to get am pm from the date time string using moment js

查看:182
本文介绍了如何使用时刻js从日期时间字符串获取pm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串 2017年3月3日星期一,上午11点/下午我必须将其转换为类似的字符串上午11:00 / PM 使用时刻js。

I have a string as Mon 03-Jul-2017, 11:00 AM/PM and I have to convert this into a string like 11:00 AM/PM using moment js.

这里的问题是我无法获得 AM <来自日期时间字符串的/ code>或 PM

The problem here is that I am unable to get AM or PM from the date time string.

我这样做:

moment(Mon 03-Jul-2017, 11:00 AM, 'dd-mm-yyyy hh:mm').format('hh:mm A')

它工作正常,因为我得到上午11:00 但是如果字符串中包含 PM ,它仍然会给出 AM 在输出中。

and it is working fine as I am getting 11:00 AM but if the string has PM in it it is still giving AM in the output.

喜欢这个时刻(2017年3月3日至7月,晚上11点,'dd-mm-yyyy hh:mm ')。format('hh:mm A')还在输出中提供 11:00 而不是 11 :00 PM

like this moment(Mon 03-Jul-2017, 11:00 PM, 'dd-mm-yyyy hh:mm').format('hh:mm A') is also giving 11:00 AM in output instead of 11:00 PM

推荐答案

解析输入时使用的格式标记错误。您应该使用 ddd 作为星期几名称的缩写, DD 表示每月的某一天, MMM 表示当月的名称缩写, YYYY ,年份 hh 1-12 小时, mm 分钟和 A 适用于上午/下午。请参见 时刻(String,String) docs。

You are using the wrong format tokens when parsing your input. You should use ddd for an abbreviation of the name of day of the week, DD for day of the month, MMM for an abbreviation of the month's name, YYYY for the year, hh for the 1-12 hour, mm for minutes and A for AM/PM. See moment(String, String) docs.

这是一个有效的实时样本:

Here is a working live sample:

console.log( moment('Mon 03-Jul-2017, 11:00 AM', 'ddd DD-MMM-YYYY, hh:mm A').format('hh:mm A') );
console.log( moment('Mon 03-Jul-2017, 11:00 PM', 'ddd DD-MMM-YYYY, hh:mm A').format('hh:mm A') );

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

这篇关于如何使用时刻js从日期时间字符串获取pm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆