Forging Titanium Episode 12: Titanium & Date.js

Row of server cabinets with computer and digital displays full of data, numbers, and blue blinking lights and leds, arranged in circular rows. Computer servers fill a room of a futuristic data center, used as a cloud computing and data storage facility. Room is illuminated by blue light. Low angle view. Supercomputer simulation, digitally generated image.

To keep the ball rolling on integrating existing Javascript libraries with Titanium, this week’s episode shows off the Date.js library in a cross-platform mobile app. We’ll include this powerful date and time processing library, in a modular manner, to enhance the native Javascript Date object. Ideally, you’ll let Date.js do all the grunt work of parsing disparately formatted dates while you spend your valuable time adding much more interesting features to your Titanium apps.

Episode Highlights

  • Introduction to the Date.js workhorse function, Date.parse()
  • Including existing Javascript libraries with CommonJS, even when they don’t export a module
  • Using the Titanium.UI.Picker to display a native picker component as each platform’s users would expect

Episode Resources:

10 COMMENTS

  1. @Serge: Typically I would see that as a bad thing as well, but the needs of date formatting have not changed dramatically over that period of time. The library built a rock solid core on its improved date parsing and provided a _ton_ of functions to modify, display, and compare.

    I guess the question is, what do you think is missing from Date.js?

  2. you really need to download date.js out of it’s subversion repo if you want a working version as i found some weirdness with just putting “12am” into the parse function until i got the very latest check in.

  3. When I TI.Include date.js, and run the simulator in debug mode, I get uncaught exceptions here:

    rtoken: function (r) { // regex token
    return function (s) {
    var mx = s.match(r);
    if (mx) {
    return ([ mx[0], s.substring(mx[0].length) ]);
    } else {
    throw new $P.Exception(s); // <– Exception Here
    }
    };
    },

    Not sure why, since two levels up the stack, there is a catch(e) clause. This doesn't happen in a pure web project. is it a Titanium bug?

  4. I’ve installed the app on my Android device, but it errors all the time at:

    Location: [113,21] app.js
    Message: “Uncaught RangeError: toString() radix arguement must be between 2 and 36”
    Source: label.text = date.toString(‘dddd.MMMM dd,yyyy h:mm:ss tt’);

    Do you have a working version or any ideas for what might be wrong?

  5. Better option.

    http://momentjs.com/

    It is uptodate and maintained. I had lots of problems with datejs. Including different versions called the same thing and an inability to parse their own iso date strings. I’ve had no issues with momentjs. Simply require it:

    var moment = require(‘/lib/thirdParty/moment.min’);

Comments are closed.