Samsung Electronics logo

Retrieving Date and Time

Learning how to handle date and time in your application is a basic locale management skill:

  1. To get the current date and time, use the getCurrentDateTime() method, which returns a TZDate object:

    var current_dt = webapis.time.getCurrentDateTime();
    console.log("Current time / date is " + current_dt.toLocaleString());
  2. To handle time zone information:
    1. To retrieve the current time zone, use the getLocalTimezone() method:

      console.log("The current time zone is " + webapis.time.getLocalTimezone());

      The general format of the time zones is "general descriptor/specific descriptor 1/specific descriptor 2/specific descriptor n". For example, "America/Argentina/Buenos_Aires".

    2. To get the number of available time zones, use the getAvailableTimezones() method:

      var tzids = webapis.time.getAvailableTimezones();
      console.log("The device supports " + tzids.length + " time zones.");
  3. If you are creating a calendar-based application or accepting a date on an application form, you must validate user input for leap year date value. For example, 29/02/2011 is an invalid user input.

    To determine if the year is a leap year, use the isLeapYear() method:

    var current_dt = webapis.time.getCurrentDateTime();
    var is_leap = webapis.time.isLeapYear(current_dt.getFullYear());
    if (is_leap)
       console.log("This year is a leap year.");
  4. The getFullYear() method returns the year (4 digits) of the TZDate object.