Get current date/time Angularjs

Asked

Viewed 6,814 times

0

I need to display on the page the current date followed by the current time, It follows what I have done so far, I only need to display the time if it continues as it 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">

Currently playing: 15/4/2016 -

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

Anyone can help?

2 answers

2


The 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 filter date

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 parameter the date format.

Follow an example on Plunker.

1

Following 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());
  • If you’re going to use pure javascript, this is the shape. Now as you’re using Angular, you can use the tools it offers you. And one question, why are you using jQuery to update the view data?

  • I gave the example of pure javascript, but I just discovered the Moment.js that made it easy and easy.. Following link: http://momentjs.com/

  • 1

    Look at my answer. Angular gives native support to date format

Browser other questions tagged

You are not signed in. Login or sign up in order to post.