phpdateformat到时刻js格式
我有一个php dateformats的配置
'dateFormat' => 'd.m.Y', 'timeFormat' => 'H:i', 'dateTimeFormat' => 'd.m.Y H:i',
但对于datetimepicker我需要moment.js格式化(http://momentjs.com/docs/#/displaying/format/),如下所示:
DD.MM.YYYY HH:mm DD.MM.YYYY HH:mm
这将是没有问题的,我来代替d
用DD
和m
用MM
,但我想知道如果没有人之前已建成的东西做到这一点.
1> Rene Vorndra..:
所以我写了一个litte helper函数将php dateformats转换为moment.js所需的格式
function convertPHPToMomentFormat($format) { $replacements = [ 'd' => 'DD', 'D' => 'ddd', 'j' => 'D', 'l' => 'dddd', 'N' => 'E', 'S' => 'o', 'w' => 'e', 'z' => 'DDD', 'W' => 'W', 'F' => 'MMMM', 'm' => 'MM', 'M' => 'MMM', 'n' => 'M', 't' => '', // no equivalent 'L' => '', // no equivalent 'o' => 'YYYY', 'Y' => 'YYYY', 'y' => 'YY', 'a' => 'a', 'A' => 'A', 'B' => '', // no equivalent 'g' => 'h', 'G' => 'H', 'h' => 'hh', 'H' => 'HH', 'i' => 'mm', 's' => 'ss', 'u' => 'SSS', 'e' => 'zz', // deprecated since version 1.6.0 of moment.js 'I' => '', // no equivalent 'O' => '', // no equivalent 'P' => '', // no equivalent 'T' => '', // no equivalent 'Z' => '', // no equivalent 'c' => '', // no equivalent 'r' => '', // no equivalent 'U' => 'X', ]; $momentFormat = strtr($format, $replacements); return $momentFormat; }
PHP之友评论