angularjssteroidsappgyversupersonic

Dynamically change navbar text


I'm building an hybrid app for iOS and Android, and there is a section where I want to show the day. If you open the app on Tuesday, it will say Tuesday and so on. It is in spanish, so, where it says "Miércoles" it is Wednesday, but I wrote it manually, I want it to change so if today is Wednesday, it automatically changes.

I tried to change it using ng-bind and the following code:

    var today = new Date();
  if(today.getDay() == 0){
    var hoy = "Domingo";
  } else if(today.getDay() == 1){
    var hoy = "Lunes";
  }else if(today.getDay() == 2){
    var hoy = "Martes";
  }else if(today.getDay() == 3){
    var hoy = "Miercoles";
  }else if(today.getDay() == 4){
    var hoy = "Jueves";
  }else if(today.getDay() == 5){
    var hoy = "Viernes";
  }else if(today.getDay() == 6){
    var hoy = "Sábado";
  }
  $scrope.variable = hoy;

It didn't work, no console errors or warnings.

Image showing my app


Solution

  • You're declaring the same variable multiple times so the declarations after the first are ignored.

    It would look nicer to use a weekday array like so:

    var now = new Date();
    //var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var weekday = ["Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes","Sábado"];
    
    var hoy = weekday[now.getDay()];
    $scope.variable = hoy;