Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

javascript / moment.js - display time since birth date

  • 06-09-2017 7:21am
    #1
    Registered Users, Registered Users 2 Posts: 599 ✭✭✭


    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,605 ✭✭✭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,262 ✭✭✭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