我在html中有一个日期时间字符串对象。日期时间字符串的格式为mm / dd / YYYY hh:mm:ss 根据月份编号,我试图获得月份名称。
我尝试了以下事情,
<div ng-switch="item.applicationData.EventStartDateTime.split("/")[0]">
JavaScript无法在角度切换表达式中使用。<span class="calendar_mon">{{item.applicationData.EventStartDateTime.split("/")[0]=="12"?"December"}}</span>
任何帮助?
答案 0 :(得分:0)
在控制器中创建一个以日期为参数的函数,查找月份并返回月份名称并在控制器中使用它,如:
<span class="calendar_mon">
{{someFunction(item.applicationData.EventStartDateTime)}}
</span>
然后在你的控制器中执行上面使用的逻辑:
$scope.someFunction = function(stringDate) {
var month = stringDate.split("/")[0];
var returnMonth;
switch(month) {
case "01": returnMonth = "January"; break;
case "02": returnMonth = "February"; break;
case "03": returnMonth = "March"; break;
case "04": returnMonth = "April"; break;
case "05": returnMonth = "May"; break;
case "06": returnMonth = "June"; break;
case "07": returnMonth = "July"; break;
case "08": returnMonth = "August"; break;
case "09": returnMonth = "September"; break;
case "10": returnMonth = "October"; break;
case "11": returnMonth = "November"; break;
case "12": returnMonth = "December"; break;
}
return returnMonth;
}
甚至更好,解析日期并获得月份:
$scope.someFunction = function(stringDate) {
var parsedDate = new Date(stringDate);
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return months[parsedDate.getMonth()];
);
或其他方式:
{{ someFunction(item.applicationData.EventStartDateTime) | date: "MMMM"}}
然后控制器中的函数看起来像:
$scope.someFunction = function(dateString) {
// you should add checks here if "dateString.split("/")[0]" returns anything
return new Date(dateString.split("/")[0]);
}
答案 1 :(得分:0)
您可以在控制器中使用返回月份的功能。
$scope.getMonthString = function(dateString){
return $filter('date')(new Date(dateString), 'MMMM');
};
然后您的HTML将
<span>
{{getMonthString(item.applicationData.EventStartDateTime)}}
</span>