如何转换日期和AngularJS或Javascript中的时间格式?

时间:2015-12-17 05:33:30

标签: javascript angularjs

(x < x ? 1 : -1) == -1

如何转换日期&amp;使用AngularJS或Javascript的时间格式?

5 个答案:

答案 0 :(得分:2)

你走了:

var date = "2015-12-16";
var dateInstance = new Date(date);

现在将$filter注入你想要的控制器/服务器/ diretive中,然后像这样写:

$filter("date")(dateInstance, "MMM dd yyyy") == "16 Dec 2015"

如果要在HTML中执行此操作,请将其绑定到$scope

$scope.dateInstance = dateInstance;

并在HTML中:

{{dateInstance | date: 'MMM dd yyyy'}}

答案 1 :(得分:2)

这将对您有所帮助:

var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var date = new Date();
var monthIndex = date.getMonth() + 1;
var minute = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
var hour = date.getHours();
var amPm = "am";

if (date.getHours() == 12) {
    amPm = "pm";
}

if (date.getHours() > 12) {
    hour = date.getHours() % 12;
    amPm = "pm";
}

var time = hour + ":" + minute + amPm;
/* getting time*/
var fullDay = date.getDate() + " " + month[monthIndex - 1] + " " + date.getFullYear()

答案 2 :(得分:1)

当您将值发送到视图(UI)时,可以在angularJs中使用过滤器,如下所示

{{ user.creation_time | date:'medium' }}

您将在用户界面中获得正确的日期。

答案 3 :(得分:1)

var arr = ["jan","feb","march","april","may","june","july","aug","sep","oct","nov","dec"];

var d = new Date();

var month = arr[d.getMonth()];
var day   = d.getDate();

var year  = d.getFullYear();


console.log(day +"-" + month + "-" + year);


var hour = d.getHours();
var min = d.getMinutes();

var temp = "AM";

if(parseInt(hour)  > 12 ){
  hour = hour % 12 ;
  temp = "PM"; 
}

console.log(hour + ":" + min + " " + temp);

答案 4 :(得分:0)

<script>
  angular.module('app', [])
    .controller('testCtrl', ['$scope', function($scope){
      var months =  ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
      var d = "2015-12-16";
      var date = new Date(d);
      var day   = date.getDate();
      var year  = date.getFullYear();


      console.log(day +"-" + months[date.getMonth()] + "-" + year);
    }])
</script>

plunker code here