How to get current timestamp of firebase server in milliseconds?

前端 未结 3 560
[愿得一人]
[愿得一人] 2020-12-15 18:21

I wanted to get firebase current timestamp, not able to find a way to get that. I want to save child as shown in the image. And for that I\'ve to get timestamp and get the d

3条回答
  •  伪装坚强ぢ
    2020-12-15 18:42

    On Android I did it this way

    index.js / serverside

    const functions = require('firebase-functions'); 
    exports.stamp = functions.https.onCall(() => {
           var d = new Date();
           console.log('TimeStamp_now : '+d.getTime());
           return { timeStamp: d.getTime() };          
       });
    

    someclass.kt / clientside

    lateinit var functions: FirebaseFunctions
    var ret :Long = 0
    
    FirebaseApp.initializeApp(this)
    functions = FirebaseFunctions.getInstance()
    
    
    val returnFC = functions.getHttpsCallable("stamp").call()
        returnFC.continueWith { task ->
        val resultFC = task.result?.data as Map
            resultFC["timeStamp"] as Long
        ret = "${resultFC["timeStamp"]}".toLong()
        val cal = Calendar.getInstance()
            cal.timeInMillis = "$ret".toLong()
        var date = DateFormat.format("dd-MM-yyyy", cal).toString()
        Log.d("current date:", date)
        Log.d("timeStamp_from_function :", "$ret")
        resultFC
    }
        returnFC.addOnSuccessListener {
            Log.d("OnSuccessListener :", "success")
    }    
        returnFC.addOnFailureListener {
            Log.d("OnFailureListener:", "failure")
    }
    

    I got the return and arrived at: TimestampConvert look great

提交回复
热议问题