Linushg111
Linushg111

Reputation: 76

Javascript countdown without Year

So I found this code on this site but there's one thing I want to change about it, but I can't seem to figure it out so I need some help. What I want to do is to remove the year in the date, so that the countdown corresponds the year that it is.

<script>
var end = new Date('04/19/2017 10:1 AM');

    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;

    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {

            clearInterval(timer);
            document.getElementById('countdown').innerHTML = 'EXPIRED!';

            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);

        document.getElementById('countdown').innerHTML = days + 'days ';
        document.getElementById('countdown').innerHTML += hours + 'hrs ';
        document.getElementById('countdown').innerHTML += minutes + 'mins ';
        document.getElementById('countdown').innerHTML += seconds + 'secs';
    }

    timer = setInterval(showRemaining, 1000);
</script>
<div id="countdown"></div>

Credit for the code goes out to this guy

Upvotes: 0

Views: 257

Answers (2)

RobG
RobG

Reputation: 147363

You should not parse strings with the Date constructor, they should be manually parsed with either a small function or library.

Assuming you have a date object, the year can be set using setFullYear, so:

// Create a date for 25 March, 2006
var d = new Date(2006, 2, 25);
console.log(d.toString());

// Set to current year
d.setFullYear(new Date().getFullYear());

console.log(d.toString());

If the date doesn't exist in the new year, e.g. if the starting date was 29 February 2016, then it rolls over to the next day, 1 March 2017.

If you know the date parts, you can go the other way and create a date and set the month and date, e.g.

// Create a date for 30 June in the current year
var d = new Date();
d.setMonth(5,30);

// Zero the time component
d.setHours(0,0,0,0);

console.log(d.toString());

Upvotes: 0

Kaique Garcia
Kaique Garcia

Reputation: 528

All you have to do is to create a new Date instance without any parameters. This will give you a Date object with the current datetime. But this is very insecure because your information will come from the browser (the client's machine). You should generate this on the server.

Whatever, if you don't know how to create that instance in Javascript, here you go:

var currentDatetime = new Date();
var currentYear = currentDatetime.getFullYear();

And then you can do something like this:

var end = new Date('04/19/' + currentYear + ' 10:1 AM');

You can explore what that Date class can do here: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 2

Related Questions