无法在Android中获得Phone Gap给我当前时间

时间:2012-03-08 07:44:08

标签: javascript android mobile cordova

我正在使用Phone Gap开发Android应用程序,我在javascript文件中获取当前时间时遇到问题。

我尝试过使用标准的Date()调用,但什么都没得到。

function getCurrentTime(){
//get current time
var currentTime = new Date();
var timestamp = JSON.stringify(currentTime);
console.log('current time: ', timestamp);
}

打印"current time: "

我在phonegap文档中找到的唯一其他参考是:

var onSuccess = function(position) {
    alert('Latitude: '          + position.coords.latitude          + '\n' +
          'Longitude: '         + position.coords.longitude         + '\n' +
**********'Timestamp: '         + new Date(position.timestamp)      + '\n');
};

//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}

navigator.geolocation.getCurrentPosition(onSuccess, onError);

我尝试过使用它,但它仍然没有打印。有什么想法吗?

    var success = function(p) {
        bkme.flags.gettingLocation = false;
        bkme.data.geolocation = {
            'lat' : p.coords.latitude,
            'lon' : p.coords.longitude,
            'accuracy' : p.coords.accuracy,
            'valid' : true
        };

        console.log('time:', new Date(p.timestamp));


    };
    var fail = function(error) { };

    navigator.geolocation.getCurrentPosition(success, fail);

2 个答案:

答案 0 :(得分:2)

这是我用于我的应用程序,它工作正常

var date = new Date();
var hour = date.getHours();
var min = date.getMinutes();
var seconds = date.getSeconds();

希望有所帮助

答案 1 :(得分:0)

您是否尝试过跳过JSON.stringify来电?