使用 AM 和 AM 将 24 小时时间转换为 12 小时时间使用 JavaScript 进行私信
使用 AM & 将以下 JSON 返回值从 24 小时格式转换为 12 小时格式的最佳方法是什么?下午?日期应保持不变 - 时间是唯一需要格式化的内容。
February 04, 2011 19:00:00
PS 如果使用 jQuery 更容易的话!也更喜欢简单的函数/代码,而不是使用 Date.js。
What is the best way to convert the following JSON returned value from a 24-hour format to 12-hour format w/ AM & PM? The date should stay the same - the time is the only thing that needs formatting.
February 04, 2011 19:00:00
P.S. Using jQuery if that makes it any easier! Would also prefer a simple function/code and not use Date.js.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
这是无需 if 语句即可更改时间的方法:
This is how you can change hours without if statement:
更新2:没有秒选项
更新:中午后上午修正,测试:http://jsfiddle.net/aorcsik/xbtjE/
我创建了这个函数来执行此操作:
UPDATE 2: without seconds option
UPDATE: AM after noon corrected, tested: http://jsfiddle.net/aorcsik/xbtjE/
I created this function to do this:
对于那些只需要输出中的时间的读者,您可以将选项传递给 JavaScript 的 Date::toLocaleString() 方法。例子:
timeString 将设置为:
8:00 AM
如果您也需要秒,请将“秒:'数字'”添加到您的选项中。对于所有选项,请参阅此。
For anyone reading who wants ONLY the time in the output, you can pass options to JavaScript's Date::toLocaleString() method. Example:
timeString will be set to:
8:00 AM
Add "second: 'numeric'" to your options if you want seconds too. For all option see this.
这是使用原型的一种相当简洁的方法:
然后您所要做的就是将字符串值转换为日期并使用新方法:
或者在一行中:
Here's a reasonably terse way to do it using a Prototype:
Then all you have to do is convert your string value to a date and use the new method:
Or in a single line:
保持简单干净
https://jsfiddle.net/rinu6200/3dkdxaad/#base
Keep it simple and clean
https://jsfiddle.net/rinu6200/3dkdxaad/#base
jQuery 根本没有任何日期实用程序。如果您不使用任何其他库,通常的方法是创建一个 JavaScript
Date
对象,然后从中提取数据并自行格式化。要创建
Date
对象,您可以确保 JSON 中的日期字符串采用Date
可以理解的形式,这是 IETF 标准(基本上是 RFC 822 第 5 节)。因此,如果您有机会更改 JSON,那将是最简单的。 (编辑:您的格式实际上可能按原样工作。)如果您无法更改 JSON,那么您需要自己解析字符串并获取日、口、年、小时、分钟和秒作为整数并创建
Date
对象。获得
Date
对象后,您需要提取所需的数据并对其进行格式化:jQuery doesn't have any Date utilities at all. If you don't use any additional libraries, the usual way is to create a JavaScript
Date
object and then extract the data from it and format it yourself.For creating the
Date
object you can either make sure that your date string in the JSON is in a form thatDate
understands, which is IETF standard (which is basically RFC 822 section 5). So if you have the chance to change your JSON, that would be easiest. (EDIT: Your format may actually work the way it is.)If you can't change your JSON, then you'll need to parse the string yourself and get day, mouth, year, hours, minutes and seconds as integers and create the
Date
object with that.Once you have your
Date
object you'll need to extract the data you need and format it:1) 用于使 24 小时变成 12 小时的“平方”指令:
2) 在一行中(相同的结果,但算法略有不同):
两个选项都返回字符串,例如
"5 pm"
或 <代码>“上午10点”等。1) "Squared" instructions for making 24-hours became 12-hours:
2) In a single line (same result with slightly different algorythm):
Both options return string, like
"5 pm"
or"10 am"
etc.您可以查看这个。其中一个例子说:
一旦你有了 Date 对象,你就可以相当容易地使用它。您可以调用 toLocaleDateString、toLocaleTimeString,也可以测试 getHours 是否大于 12,然后计算 AM/PM 时间。
You can take a look at this. One of the examples says:
Once you have Date object you can fairly easy play with it. You can either call toLocaleDateString, toLocaleTimeString or you can test if getHours is bigger than 12 and then just calculate AM/PM time.
编辑:我忘记处理上午/下午 12 点。固定的。
edit: I forgot to deal with 12 o'clock am/pm. Fixed.
这是演示
Here is the Demo
请尝试使用下面的代码
谢谢
Please try with below code
Thanks
这对我有用!
This worked for me!
您可以尝试这个更通用的函数:
返回灵活的对象格式。
https://jsbin.com/vexejanovo/edit
You could try this more generic function:
Returns a flexible object format.
https://jsbin.com/vexejanovo/edit
我是一个相对新手,但这是我为自己的一个项目想到的,而且它似乎有效。可能有更简单的方法可以做到这一点。
I'm a relative newbie, but here's what I came up with for one of my own projects, and it seems to work. There may be simpler ways to do it.
这是您的 html 代码,您在其中调用函数将 24 小时时间格式转换为 12 小时(上午/下午)
现在在js代码中按原样编写这个tConvert函数
将 18:00:00 转换为 6:00:00PM 为我工作
this is your html code where you are calling function to convert 24 hour time format to 12 hour with am/pm
now in js code write this tConvert function as it is
converting 18:00:00 to 6:00:00PM working for me
该函数将在两个方向上进行转换:
12 到 24 小时 或 24 到 12 小时
以下是一些示例:
This function will convert in both directions:
12 to 24 hour or 24 to 12 hour
Here's some examples:
给你
,这是你的转换器函数;)祝你编码愉快!
像这样调用它
ConvertTo12Hrs("2011 年 2 月 4 日 19:00:00");它将返回您的对象,您可以使用该对象按照您的喜好格式化回日期时间字符串...
Here you go
And here's the converter func for yas ;) Happy coding!!
invoke it like
convertTo12Hrs("February 04, 2011 19:00:00"); it will return you the object, which in turn you can use to format back your datetime string as you fancy...
无论如何,你最终都会进行大量的字符串操作,
那么为什么不直接操作日期字符串本身呢?
浏览器对日期字符串的格式不同。
所以你必须检查一下。
这会将 D 设置为 24 小时 HH:MM:SS 字符串。将其拆分为
冒号,第一个元素是小时。
您可以将 24 小时制转换为 12 小时制,但这并没有
这里其实已经提到了。可能是因为它相当疯狂
当你转换小时数时,你实际上在数学上做什么
从时钟。事实上,你所做的就是添加 23,然后对其进行修改
乘以 12,然后加 1
因此,例如,您可以从日期字符串 mod 中获取整个时间
小时,并用新的小时显示所有内容。
使用一些智能正则表达式,您可能可以从 HH:MM:SS 中提取时间
日期字符串的一部分,并将它们全部放在同一行中。这将是
荒谬的一行,因为反向引用 $1 不能用于
无需在替换中放置函数即可进行计算。
看起来是这样的:
正如我所说,这很荒谬。如果您使用的库可以执行
如果您对反向引用进行计算,则该行将变为:
如果您很好地记录了它,那么这实际上并不是不可能作为可用代码。
该行说:
故事的重点是,如何将 24 小时制时间转换为 12 小时制时间
是一种非显而易见的数学计算:
将 23 加起来,再除以 12,然后再加一个。
You're going to end up doing alot of string manipulation anyway,
so why not just manipulate the date string itself?
Browsers format the date string differently.
so you'll have to check for that.
That will set D equal to the 24-hour HH:MM:SS string. Split that on the
colons, and the first element will be the hours.
You can convert 24-hour hours into 12-hour hours, but that hasn't
actually been mentioned here. Probably because it's fairly CRAZY
what you're actually doing mathematically when you convert hours
from clocks. In fact, what you're doing is adding 23, mod'ing that
by 12, and adding 1
So, for example, you could grab the whole time from the date string, mod
the hours, and display all that with the new hours.
With some smart regex, you can probably pull the hours off the HH:MM:SS
part of the date string, and mod them all in the same line. It would be
a ridiculous line because the backreference $1 couldn't be used in
calculations without putting a function in the replace.
Here's how that would look:
Which, as I say, is ridiculous. If you're using a library that CAN perform
calculations on backreferences, the line becomes:
And that's not actually out of the question as useable code, if you document it well.
That line says:
Point of the story is, the way to convert 24-hour-clock hours to 12-hour-clock hours
is a non-obvious mathematical calculation:
You add 23, mod by 12, then add one more.
这是一个对我有用的不错的小功能。
Here is a nice little function that worked for me.
我注意到已经有一个答案,但我想分享我自己的解决方案,使用纯 JavaScript:
您可以在 https://jsfiddle.net/j2xk312m/3/ 使用以下代码块:
I noticed there is already an answer, but I wanted to share my own solution, using pure JavaScript:
You can see it in action at https://jsfiddle.net/j2xk312m/3/ using the following code block:
这样您就可以更好地控制输出 - 即 - 如果您希望时间格式为 '4:30 pm' 而不是 '04:30 PM' - 您可以转换为您想要的任何格式 - 也可以稍后更改。而不是受限于一些不允许任何灵活性的旧方法。
您只需转换前 2 位数字,因为 24 小时制或 12 小时制的分和秒数字相同。
This way you have more control over the output - i.e - if you wanted the time format to be '4:30 pm' instead of '04:30 P.M.' - you can convert to whatever format you decide you want - and change it later too. Instead of being constrained to some old method that does not allow any flexibility.
and you only need to convert the first 2 digits as the minute and seconds digits are the same in 24 hour time or 12 hour time.
JavaScript 中理想的简单答案,不包括秒。
Ideal simplistic answer in JavaScript, excluding seconds.