0

I am using Cordova/Phonegap for a cross platform application where I need to format a JSON date that has been returned from an AJAX call.

The date is stored as UTC and I need to format it for the local timezone. So it seems that cordova's navigator.globalization.dateToString would fit the bill. However, I am building a larger string of data, so I can't handle the complete job in the callback. I need to get the formatted date/time in a string and use it later.

   var myTime = new Date(parseInt(myData.StartTime.substr(6)));  // get a date object

    var myTimeString = '';
    console.log("before");
    navigator.globalization.dateToString(myTime,
            function(date)
            {
            console.log("doing");
            myTimeString = date.value;
        },
            function()
            {
            alert('Error getting dateString\n');
             },
             {
                  formatLength : 'short',
                  selector : 'date and time'
             });
    console.log("done");
    console.log(myTimeString);    // the problem is that this is not necessary set.

For Android, the code above yields: before doing done

For iOS, the above code yields: before done doing

I think the problem is that the anonymous function is asynchronous and may or may not be set on by the time the console.log() is executed.

Is there a generally accepted way to resolve this timing issue? Or is there a better way to get what I want / need?

---EDIT---

Based on the below, I realized that I have asked the wrong question.

First, I've refactored my code so that the output can be handled from the handler function. Thank you for pointing this out. It is much cleaner.

However, I need to do this in a loop and need to keep track of which iteration I'm working with.

for(var i = 0; i < 5; i++)
{
    var myTime = new Date(parseInt(myData.StartTime.substr(6)));  // get a date object
    navigator.globalization.dateToString(myTime, 
          function(date) {
            myTimeString = "my date is: " +date.value  +" my iterator is: "+i;
            console.log(myTimeString);
          },
          function() { alert('Error getting dateString\n'); },
          {
            formatLength : 'short',
            selector : 'date and time'
           });
}

In android, I get something like

  • my date is: some date my iterator is: 0
  • my date is: some date my iterator is: 1
  • my date is: some date my iterator is: 2
  • my date is: some date my iterator is: 3
  • my date is: some date my iterator is: 4

In iOS, I get

  • my date is: some date my iterator is: 5
  • my date is: some date my iterator is: 5
  • my date is: some date my iterator is: 5
  • my date is: some date my iterator is: 5
  • my date is: some date my iterator is: 5

I want to be able to do the former example, what am I missing?

Jeff G
  • 89
  • 1
  • 11
  • 1
    Asked and answered here:http://stackoverflow.com/questions/7433824/how-to-get-current-loop-iteration-from-anonymous-function-within-the-for-loop – Jeff G Jun 18 '13 at 04:33

1 Answers1

0

Yes it's an asynchronous call so you need to continue your process in the callback function :

var myTime = new Date(parseInt(myData.StartTime.substr(6)));  // get a date object

var myTimeString = '';
console.log("before");
navigator.globalization.dateToString(myTime,
        function(date)
        {
           console.log("doing");
           myTimeString = date.value;
           callback(myTimeString);
        },
        function()
        {
            alert('Error getting dateString\n');
        },
        {
          formatLength : 'short',
          selector : 'date and time'
        });

function callback(myTime)
{
    console.log("done");
    console.log(myTime);
}
gaepi
  • 404
  • 3
  • 14
  • What's the difference between the date/time returned by JavaScript when run on a PhoneGap App on a phone, and the date/time returned by PhoneGap's globalization functions when run on that same phone? – Matty J Dec 02 '13 at 22:43