Search code examples
iosdatetimetitaniumnsdateformattertitanium-mobile

NSDateFormatter equivalent in Titanium / Convert date in specified format in Titanium


I'm working with the date and time in my Titanium based iOS application.

My issue is, I'm getting the time in the below format:

Wed Jan 16 2013 17:32:40 GMT+0530 (IST)

I want to change it like:

Jan 16, 2013, 05:32 pm

In iOS there is NSDateFormatter for doing this.

What I did :

  1. Currently I'm doing it manually using split and switch cases, but it fails if the input time format changes, then I need to re-write the code for the changed format.
  2. I searched alot in Titanium Docs, but didn't get any solution.
  3. I asked same question on their forum, didn't get any reply till now.

Is there is anyway to do this in Titanium?

Please help, Thanks in advance.


Solution

  • I didn't find any alternatives for the NSDateFormatter in Titanium.

    I tested the Moment.js, but it didn't worked for my case.

    I implemented a JavaScript function for achieving my output.

    /**
     * Function for formatting the date and time 
     * @param {Object} dateTime 
     */
    function formatDateTime(dateTime)
    {
        var arr = dateTime.split(' ');
        var convertedDate = arr[1]+' '+arr[2]+', '+arr[3];
        var convertedTime = arr[4];
        var returnValue = convertedDate+', '+convertedTime;
        return returnValue;
    }
    

    Edit on 27 Feb 2014

    After facing the JavaScript date formatting issue on different occasions, I finally developed my own JavaScript module, this module will format the JavaScript Date object to Specified formats. You can find it here : MMP_DateTime