Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Get current date and time.

Hello,

How can I get current date and time on the device that the application is running?

Thanks!

— asked March 30th 2010 by Skinkers Developer
0 Comments

7 Answers

  • try it like this:

    function getDate(0 {
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    
    return month+"/"+day+"/"+yeah+" -  "hours +":"+minutes;
    
    }
    

    if you call the function getDate(); it will print teh date/time like this: 3/30/2010 - 13:27

    — answered March 30th 2010 by Glenn Tillemans
    permalink
    3 Comments
    • should be 'year' not 'yeah' in the return line ;-)

      — commented July 6th 2010 by Rolf Dohrmann
    • 0 should be replaced by ')'

      — commented October 15th 2013 by Anjali Bhavsar
    • Fixed:

      function getDate() {
          var currentTime = new Date();
          var hours = currentTime.getHours();
          var minutes = currentTime.getMinutes();
          var month = currentTime.getMonth() + 1;
          var day = currentTime.getDate();
          var year = currentTime.getFullYear();
      
          return month+"/"+day+"/"+year+" - "+hours +":"+minutes;
      };
      

      — commented May 28th 2014 by Dan Peleg
  • I haven't had any problems at all using the javascript Date object:

    var d = new Date();
    
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    

    See http://www.w3schools.com/jsref/jsref_obj_date.asp.

    — answered March 30th 2010 by James K
    permalink
    0 Comments
  • I cleaned up the getDate() function:

    
        function getDate() {
        var currentTime = new Date();
        var hours = currentTime.getHours();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        var month = currentTime.getMonth() + 1;
        var day = currentTime.getDate();
        var year = currentTime.getFullYear();
    
        if (hours < 10) { hours = "0" + hours};    
        if (minutes < 10) { minutes = "0" + minutes};
        if (seconds < 10) { seconds = "0" + seconds};
    
        return month + "/" + day + "/" + year + " -  " + hours + ":" + minutes + ":" + seconds;
    
        }
    
    — answered January 4th 2013 by David Zahn
    permalink
    0 Comments
  • Thanks,

    I have just tested this on a device. I have changed date, time and timezone few times and it is working as expected.

    — answered March 30th 2010 by Skinkers Developer
    permalink
    0 Comments
  • Hi

    I use the following reference:

    http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm

    Check it out!

    — answered March 30th 2010 by Peter Lum
    permalink
    0 Comments
  • But however thanks for posting the solution.

    — answered May 5th 2010 by Markus Shpit
    permalink
    0 Comments
  • I have done this with form builder.

    — answered May 5th 2010 by Markus Shpit
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.