2

I'm trying to get Parse server date and time but I get to know the only way is to create or update an object then retrieve the value of updatedAt.

My class

//There is only one data/object in this class

Class Test{
  int times
};

My cloud code

increment of value "times" is to get current server date and time via getUpdatedAt();

Parse.Cloud.define("updateTimes", function(request, response) {
  var test = Parse.Object.extend("Test");
  var query = new Parse.Query(test);
  query.first({
    success: function(results) {
      results.increment("times");
      results.save();
      response.success("success updated");
    },
    error: function() {
      response.error("test lookup failed");
    }
  });
});

My calling at android studio

//Call updateTimes function at cloud code
ParseCloud.callFunctionInBackground("updateTimes", new HashMap<String, Object>(), new FunctionCallback<String>() {
            public void done(String result, ParseException e) {
                if (e == null) { //success }
            }
        });

        //Get date and time from server
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Test");
        query.getInBackground("i6hmOEItvI", new GetCallback<ParseObject>() {
            public void done(ParseObject object, ParseException e) {
                if (e == null) {
                    // success
                    serverDate = object.getUpdatedAt();
                    Format formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                    date = formatter.format(serverDate);
                    Log.d("ServerDate",date);
            }
        });

My problem is the serverDate retrieved from objectId is not correct. The date retrieved is always the older one. However, the updateAt field in database is successfully updated. I have called ParseCloud.callFunctionInBackground before calling query object.getUpdatedAt();. Any has any idea why the date I get is not the latest?

1 Answer 1

2

It seems a waste to create or touch data just to see what date stamp was written. Why not return a date directly:

Parse.Cloud.define("serverDateTime", function(request, response) {
    response.success({dateTime: new Date()});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I actually tried using new Date() but the result returned on my emulator was the time of my device. Eventually so I thought new Date() get date from device. My bad :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.