how to have the current date with Angularjs

Asked

Viewed 8,268 times

4

How to have the return of the current date via javascript, in the time-label class can use jquery or angular.

The idea is to have the last update date.

If anyone knows I’m grateful.

You cannot enter the direct value, some function has to be created that at runtime add the date..

in an html class:

<div class="footer">
    <div class="col-md-12">
        <span class="pull-left time-label"></span>
        <div class="pull-right">
            <a href="#" class="toggle-legend visible-lg pull-left cLineSwap">Legend</a>
        </div>
    </div>
</div>
  • You want to give a class to the element that is a date? can give an example of the format you want to have?

  • Actually @Sergio, I want a function to create depending on the format. just add the current date via jquery or angular. If it helps, I saw something in that sense: $("#"+keyword_idDiv+ " .pull-left.Verizon-widget-time-label"). html("Last Updated: "+ lastUpdated); with jquery

  • Okay, and where does this date come from? Do you change in HTML? in the script? or come from the database?

  • Javascript has to generate, or use Angularjs to generate this date. It’s something dynamic.

2 answers

6


You can use it like this: http://jsfiddle.net/dZSL4/

function dataHoje() {
    var data = new Date();
    var dia = data.getDate();
    var mes = data.getMonth() + 1;
    var ano = data.getFullYear();
    return [dia, mes, ano].join('/');
}
$('.time-label').html('Ultima atualização: ' + dataHoje());

In javascript the date has to be "departure" in day, month and year to be formatted this way.

1

Good alternative would be to use Moment.js, has numerous configurations and outputs. Download the package with the Langs, so you can use the moment.lag('pt-br'); which is en (Brazil).

I’ll put what you ordered with various styles:

Html:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script language="javascript" src="jquery-1.11.0.min.js"></script>
<script language="javascript" src="js/moment-with-langs.min.js"></script>
<script language="javascript">
    $(document).ready(function(e) {
        moment.lang('pt-br');
        $("#DataAtual0").html(moment().format("L"));
        $("#DataAtual1").html(moment().format('L'));
        $("#DataAtual2").html(moment().format('l'));
        $("#DataAtual3").html(moment().format('LL'));
        $("#DataAtual4").html(moment().format('ll'));
        $("#DataAtual5").html(moment().format('LLL'));
        $("#DataAtual6").html(moment().format('lll'));
        $("#DataAtual7").html(moment().format('LLLL'));
        $("#DataAtual8").html(moment().format('llll'));
    }); 
</script>
</head>
<body>  
    <div id="DataAtual0"></div>
    <div id="DataAtual1"></div>
    <div id="DataAtual2"></div>
    <div id="DataAtual3"></div>
    <div id="DataAtual4"></div>
    <div id="DataAtual5"></div>
    <div id="DataAtual6"></div>
    <div id="DataAtual7"></div>
    <div id="DataAtual8"></div>
</body>
</html>

Upshot:

inserir a descrição da imagem aqui

Example: Jsfiddle

References:

Browser other questions tagged

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