Get current date / time AngularJS

1

I need to display on the page the current date followed by the current time, Here's what I've done so far, I just need to display the time if I continue as is, follow code:

 function dataHoje() {
            var data = new Date();
            var dia = data.getDate();
            var mes = data.getMonth() + 1;
            var ano = data.getFullYear();
            return [dia, mes, ano].join('/');
        }

Html:

 <span class="pull-left time-label">

Running: 15/4/2016 -

I just need the time, like this: 15/4/2016 - 14:55

Can anyone help?

    
asked by anonymous 15.04.2016 / 19:55

2 answers

1

Angular has several Filters to format the data. Filters can be added to expressions using the | pipe character, followed by a filter. In your case, you need to use the date filter

On your page you should call the filter this way:

{{ data| date:'dd/MM/yyyy HH:mm:ss'}}

Some filters accept parameters, the date filter is one of them. In the example we pass as format the date format.

Here's an example in the Plunker .

    
15.04.2016 / 20:23
2

Here's the answer:

 function dataHoje() {
     var data = new Date();
     var dia = data.getDate();
     var mes = data.getMonth() + 1;
     if (mes < 10) {
        mes = "0" + mes;
    }
    var ano = data.getFullYear();
    var horas = new Date().getHours();
    if (horas < 10) {
        horas = "0" + horas;
    }
    var minutos = new Date().getMinutes();
    if (minutos < 10) {
        minutos = "0" + minutos;
    }
    var result = dia+"/"+mes+"/"+ano+" - "+horas + "h" + minutos;
    return result;
}
$('.time-label').html(dataHoje());
    
15.04.2016 / 20:21