0

I have a date time string object in a html. The format of date time string is mm/dd/YYYY hh:mm:ss Based upon the month number I am trying to get month name.

I tried following things,

  1. I cannot put .getMonth() as it string.
  2. <div ng-switch="item.applicationData.EventStartDateTime.split("/")[0]"> JavaScript is not working in angular switch expression.
  3. Below is not working <span class="calendar_mon">{{item.applicationData.EventStartDateTime.split("/")[0]=="12"?"December"}}</span>
  4. All I can do is write some js operations in the object.

Any help?

1
  • You could have getMonth as function inside your ng-switch directive like ng-switch="getMonth(item.applicationData.EventStartDateTime)" & then getMonth will return a string value Commented Dec 30, 2015 at 19:28

2 Answers 2

0

Create a function in your controller that gets the date as param, find the month and returns the month name and use it in the controller like:

<span class="calendar_mon">
     {{someFunction(item.applicationData.EventStartDateTime)}}
</span>

then in your controller do the logic you used above:

$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;
}

or even better, parse the date and get the month:

$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()];
);

or another way:

{{ someFunction(item.applicationData.EventStartDateTime) | date: "MMMM"}}

then the function in the controller, would look like:

$scope.someFunction = function(dateString) {
     // you should add checks here if "dateString.split("/")[0]" returns anything
    return new Date(dateString.split("/")[0]);
}
1
  • Thanks Arber. I used the solution 2. Commented Jan 4, 2016 at 16:55
0

You can have a function in your controller that returns the month.

$scope.getMonthString = function(dateString){
   return $filter('date')(new Date(dateString), 'MMMM');
};

Then your HTML will be

<span>
    {{getMonthString(item.applicationData.EventStartDateTime)}}
</span>
1
  • Vetrivelmurugan, don't forget to inject $filter if you use this option :) Commented Dec 30, 2015 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.