Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

javascript / moment.js - display time since birth date

  • 06-09-2017 08:21AM
    #1
    Registered Users, Registered Users 2 Posts: 596 ✭✭✭


    hi,

    i am using moment.js library, what i want is a clock to display time since your birth, in format "years/months/days/hours/seconds" since your birth.

    Anyone got experience with this? i have read through the docs but cannot figure out how to get the formatting right.

    Currently i am getting:

    "time since birth - 48 years ago" with -> var result = moment(1973-09-13).fromNow();

    Incorrect.

    Anyone used this before? Got any ideas?

    thanks.


    [HTML]
    <!DOCTYPE html>
    <html>
    <title>moment.js example</title>
    <body>
    <script src="moment.js"></script>
    <head>

    </head>

    <div id="clock"></div>

    <script type="text/javascript">
    (function () {

    var clockElement = document.getElementById( "clock" );

    function updateClock ( clock ) {
    var result = moment(1973-09-13).fromNow();

    clock.innerHTML = "time since birth - " + result;
    }
    -
    setInterval(function () {
    updateClock( clockElement );
    }, 1000);

    }());
    </script>

    </body>
    </html>
    [/HTML]


Comments

  • Registered Users, Registered Users 2 Posts: 403 ✭✭counterpointaud


    You need to pass the date to moment as a string, wrap it in single-quotes


  • Registered Users, Registered Users 2 Posts: 9,534 ✭✭✭gctest50


    you could play around with it


    function updateClock ( clock ) {

    var dateA = moment('1973-09-13');
    var dateB = moment(Date.now());
    var result1= (dateB.diff(dateA,'minutes'));
    var result2= (dateB.diff(dateA,'hours'));
    var result3= (dateB.diff(dateA,'days'));
    var result4= (dateB.diff(dateA,'weeks'));
    var result5= (dateB.diff(dateA,'years'));
    clock.innerHTML = "minutes:" + result1 + " hours:" + result2 + " days:" + result3 + " weeks:" + result4 + " years:" + result5 ;




    }
    -
    setInterval(function () {
    updateClock( clockElement );
    }, 1000);


  • Registered Users, Registered Users 2 Posts: 6,271 ✭✭✭Buford T Justice


    You need to wrap your date in quotes, since its a string you are converting to a moment object, but you also need to specify a format
    <!DOCTYPE html>
    <html>
    <title>moment.js example</title>
    <body>
    	<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
    	<head>
        	
    	</head>
    
    	<div id="clock"></div>
    
         <script type="text/javascript">
    	    (function () {
    
    	      var clockElement = document.getElementById( "clock" );
    
    	      function updateClock ( clock ) {
    			var result = moment("1973-09-13", "YYYY-MM-DD").fromNow();
    
    	        clock.innerHTML = "time since birth - " + result;
    	      }
    -
    	      setInterval(function () {
    	          updateClock( clockElement );
    	      }, 1000);
    
    	    }());
      	</script>
    
    </body>
    </html>
    
    Link


Advertisement