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?

4

1 回答 1

0

是的,这是一个异步调用,因此您需要在回调函数中继续您的过程:

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);
}
于 2013-06-17T10:12:39.873 回答