Dynamically change navbar text

302 views Asked by At

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

3

There are 3 answers

1
Jecoms On BEST ANSWER

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;
0
Alisson Reinaldo Silva On

It looks you have a typo.

Change from

$scrope.variable = hoy;

to

$scope.variable = hoy;

0
Jerad Rutnam On

Simply update your code,

var today = new Date(),
    weekday = [ 
                "Domingo",
                "Lunes",
                "Martes",
                "Miercoles",
                "Jueves",
                "Viernes",
                "Sábado"
              ];

var hoy = weekday[today.getDay()];
$scope.variable = hoy;

Demo: https://jsfiddle.net/1a148yrw/2/