Javascript AngularJS 如何将字符串“yyyyMMdd”转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26676184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS how to convert string "yyyyMMdd" to date
提问by Nahed
can you help please to convert below in Angularjs I have value "20141023" and would like to convert to date in AngularJS and then displayed View in format dd/MMM/yyyy Many thanks N.
你能帮我在 Angularjs 中转换下面吗?我有值“20141023”,想在 AngularJS 中转换为日期,然后以 dd/MMM/yyyy 格式显示视图 非常感谢 N.
回答by sylwester
You can use regular expression please see demo below
您可以使用正则表达式,请参阅下面的演示
var app = angular.module('app', []);
app.controller('homeCtrl', function($scope) {
var st = "20130510";
var pattern = /(\d{4})(\d{2})(\d{2})/;
$scope.date = new Date(st.replace(pattern, '--'));
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="homeCtrl">
{{date | date :'dd/MMM/yyyy'}}
</div>
</div>
回答by Patrick Reck
I would suggest using MomentJS(see the basic examples here) along with the AngularJS wrapper.
我建议使用MomentJS(请参阅此处的基本示例)和AngularJS 包装器。
Using MomentJS you can define your format easily.
使用 MomentJS,您可以轻松定义格式。
moment().format('YYYYMMDD')
If you want to look more into it, please refer to their documentation.
如果您想深入了解它,请参阅他们的文档。

