0

i'm currently trying to format dates i have in a "list". At the end i should get 06.11.2015. 14:51 02.11.2015. 13:56 06.11.2015. 14:51 02.11.2015. 13:56

but i get 02.11.2015. 13:56 02.11.2015. 13:56 02.11.2015. 13:56 02.11.2015. 13:56

I don't want same date for each elements. I'm not getting what is wrong in my code. Here's an overview to make it more clear : jsfiddle

Thanks a lot for your help !!

<html>
     <head>
     <script type="text/javascript"  src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
     <script>
$(document).ready(function(){

var date = $('.start-date').text();
            var strArray = date.match(/(\d+)/g);
            var i = 0;
        for (i = 0; i < strArray.length; i++) 
            {
                var d = parseInt(strArray[i]);
                var dateF =  new Date (d);
                var dd = dateF.getDate();
                var mm = dateF.getMonth() + 1;
                var yyyy = dateF.getFullYear();
                var hh = dateF.getHours();
                var mn = dateF.getMinutes();
                if(dd<10){
                dd = '0' + dd
                } 
                if(mm<10){
                mm='0'+mm
                } 
                var today = dd+'.'+mm+'.'+yyyy+'. '+hh+':'+mn;
                console.log(today);


                $( ".start-date" ).each(function( index ) {
                  $( this ).text(today);
                });
            };
    });
 </script>
</head>
<body>
     <div class="start-date">/Date(1446817860000)/</div>

<div class="start-date">/Date(1446468960000)/</div>

<div class="start-date">/Date(1446817860000)/</div>

<div class="start-date">/Date(1446468960000)/</div>
</body>
</html>
0

2 Answers 2

2

Your code simply gets all the date strings concatenated together, then applies the result to that long string.

Your code belongs inside the each loop!

e.g. something like:

$(document).ready(function () {

    $(".start-date").each(function (index) {
        var date = $(this).text();
        var strArray = date.match(/(\d+)/g);
        var i = 0;
        for (i = 0; i < strArray.length; i++) {
            var d = parseInt(strArray[i]);
            var dateF = new Date(d);
            var dd = dateF.getDate();
            var mm = dateF.getMonth() + 1;
            var yyyy = dateF.getFullYear();
            var hh = dateF.getHours();
            var mn = dateF.getMinutes();
            if (dd < 10) {
                dd = '0' + dd
            }
            if (mm < 10) {
                mm = '0' + mm
            }
            var today = dd + '.' + mm + '.' + yyyy + '. ' + hh + ':' + mn;
            console.log(today);

            $(this).text(today);
        };
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

"Your code simply gets the first date" That's wrong. $('.start-date').text() returns the concatenated text of all matched elements. That's why the fiddle shows the last date
@Andreas: You'd think after 2 years of writing jQuery plugins I would have come across that before. Have corrected the comment. :)
0

You don't have to update each div on every iteration, just needed one. http://jsfiddle.net/wbz5mLdv/1/

Change:

$( ".start-date" ).each(function( index ) {
    $( this ).text(today);
});

To:

$(".start-date").eq(i).text(today); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.