gpt4 book ai didi

javascript - 将日期和时间转换为单一格式字符串的 Angular 过滤器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:49 28 4
gpt4 key购买 nike

我正在使用来自 API 的数据,它以两个不同的键/值对(日期和时间)返回日期和时间。

<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="ctrl">

Date {{date}} - Time {{time}}
<br/>
{{date | dateformatter}}

</body>
</html>
angular.module("app",[]).controller("ctrl", function($scope) {
$scope.date = "03/13/2014";
$scope.time = "8:10:56";

}).filter("dateformatter", function($filter){
// this should return 'yyyy-MM-dd h:mm:ss'
return function(dt) {
return "2014 03 13 8:10:56";
}
})

我可以使用过滤器将其转换为单一格式的字符串吗?

Here's an example in jsBin

最佳答案

我会将 DateTime 转换为 Date 对象并使用 Date filter

所以 Controller 看起来像:

app.controller("ctrl", function($scope) {
$scope.date = "03/13/2014";
$scope.time = "8:10:56";

$scope.newDate = new Date( $scope.date + ' ,' + $scope.time).getTime();
});

HTML:

{{newDate  | date: 'yyyy-MM-dd h:mm:ss'}}

演示 Fiddle

关于javascript - 将日期和时间转换为单一格式字符串的 Angular 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22985779/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com