Date.prototype.toLocaleTimeString()中的HourCycle选项之间有什么区别

时间:2019-01-18 16:57:49

标签: javascript datetime specifications

toLocaleTimeString()的MDN文档指示hourCyclehc选项具有四个可能的值:"h11""h12""h23"和&{{ 1}}。

其中两个可能的值使我感到很明显(即"h24""h12"),但是另两个,我不知道它们的作用,而我的duckduckfoo / googlefoo却使我失望了! / p>

"h24""h11"代表什么?

我最好的猜测是,它们是"h23"0的{​​{1}}与1派生的某种类型,但是基础日期戳仍然相同,并且记录的值是相同的,所以如果是,则区别在哪里?

应该在MDN's toLocalTimeString pageECMAScript's toLocalTimeString page上对此文件进行记录或至少与之链接,但事实并非如此。它确实让我印象深刻,因为它应该很容易弄清楚,但是我看不到区别,它现在正在我的皮肤下爬行!

"h12"

2 个答案:

答案 0 :(得分:2)

我发现dateStyle and timeStyle options for Intl.DateTimeFormat的提议说:

  

[[HourCycle]]是一个字符串值,指示是12小时制("h11""h12")还是24小时制("h23""h24" )应该使用。 "h11""h23"从小时0开始,分别上升到11和23。 "h12""h24"从第1小时开始,一直到12和24。[[HourCycle]]仅在[[Hour]]没有未定义时使用。

英语或美式风格可能更喜欢h12

› new Date(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h12' })
‹ "5/1/2019, 12:00:00 PM"
› new Date(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h11' })
‹ "5/1/2019, 0:00:00 PM"

h24必须谨慎使用。如果日期部分是前一天的值,那会很好。

› new Date(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h23' })
‹ "2019/5/1 0:59:59"
› new Date(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h24' })
‹ "2019/5/1 24:59:59"

Compatibility table in MDN说Firefox 58和Edge支持这一点。

答案 1 :(得分:1)

我同意目前很难找到有关import Data.Void import Text.Megaparsec import Text.Megaparsec.Char import qualified Text.Megaparsec.Char.Lexer as L import Control.Monad.Combinators.Expr type Parser = Parsec Void String -- standard boilerplate for space handling spaces :: Parser () spaces = L.space space1 empty empty lexeme :: Parser a -> Parser a lexeme = L.lexeme spaces symbol :: String -> Parser String symbol = L.symbol spaces -- parse a number number :: Parser Float number = lexeme $ try L.float <|> fromIntegral <$> L.decimal -- parse an identifier (e.g., "x_4") identifier :: Parser String identifier = lexeme $ (:) <$> (letterChar <|> char '_') <*> many (alphaNumChar <|> char '_') -- abstract syntax tree for expressions data Expr = Num Float | Var String | Negate Expr | BinOp BinOp Expr Expr deriving (Show) data BinOp = Add | Sub | Mult | Div | Power deriving (Show) -- parse a "term": number, identifier, or expression in parentheses term :: Parser Expr term = Num <$> number <|> Var <$> identifier <|> between (symbol "(") (symbol ")") expr -- parse an expression combining terms with operators expr :: Parser Expr expr = makeExprParser term [ [binaryR "^" Power] , [Prefix (Negate <$ symbol "-")] , [binary "*" Mult, binary "/" Div] , [binary "+" Add, binary "-" Sub] ] where binary name f = InfixL (BinOp f <$ symbol name) binaryR name f = InfixR (BinOp f <$ symbol name) -- parse a whole string as an expression fullExpr :: Parser Expr fullExpr = spaces *> expr <* eof main = parseTest fullExpr "(pi*r^2 - 4*pi*r) / (c^2 - a^2 - b^2)" 值的MDN文档,但我在这里找到了它们:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Locale/hourCycle

面向开发人员的网络技术> JavaScript> JavaScript参考>标准内置对象> Intl.Locale> Intl.Locale.prototype.hourCycle

[…]

h12 :小时制使用1–12;对应于模式中的“ h”。 12小时制,午夜从12:00开始 上午。

h23 :小时制,使用0–23;对应于模式中的“ H”。 24小时制,午夜从0:00开始。

h11 :小时制,使用0-11;对应模式中的“ K”。 12小时制,午夜从凌晨0:00开始。

h24 :小时制使用1–24;对应于模式中的“ k”。 24小时制,午夜从24:00开始。

相关问题