首页 > 解决方案 > Uncaught TypeError: .replace is not a function

问题描述

On my ajax request, i want to display the number of day between two date. So, if date is in the futur, he return me "In 5 days", and its perfect. But the problem its when the date was in the past, he return me "-5 days ago".

So i try to remove the dash with the .replace function, but i have this error.. Uncaught TypeError: counter.replace is not a function

 $.ajax({
                    url : 'http://localhost/json/getData?band='+bandid+'',
                    type : 'GET',
                    dataType : 'json',
                    success : function(data) {
                        $('#viewModal-band--dates').empty();
                        $.each(data, function(idx, elem) {

                            var today = new Date();
                            var date = new Date(elem.cDate);
                            var years = date.getFullYear();
                            var day = date.getDate();

                            var diff = new Date(date - today);
                            var counter = diff/1000/60/60/24;
                            var counter2 = counter.replace('-', '');

                            if(counter < 0) {
                                var sentenceCounter = 'Il y a ' + counter2.toFixed() + ' jours';
                            } else if (counter > 0) {
                                var sentenceCounter = 'Dans ' + counter2.toFixed() + ' jours';
                            }

                            $('#viewModal-band--dates').append('+counter+');
                            x++;
                        });

                        $('#viewModal-band--header-count').text(x);

                    },
                    error : function() {
                        alert('Erreur lors de la requête...');
                    }
                });

标签: jqueryajaxreplace

解决方案


推荐阅读