问题
How can I select the timescripts of my documents in Date readable format? I want to do something like this:
SELECT CAST(C._ts AS DATE) FROM C
Specific to Cosmos DB SQL Query only please.
回答1:
Please use UDF in Cosmos DB.
sample document:
udf:
function convertTime(unix_timestamp){
var date = new Date(unix_timestamp*1000);
var year = date.getFullYear();
var month = ("0"+(date.getMonth()+1)).substr(-2);
var day = ("0"+date.getDate()).substr(-2);
var hour = ("0"+date.getHours()).substr(-2);
var minutes = ("0"+date.getMinutes()).substr(-2);
var seconds = ("0"+date.getSeconds()).substr(-2);
return year+"-"+month+"-"+day+" "+hour+":"+minutes+":"+seconds;
}
SQL: SELECT udf.convertTime(c._ts) FROM c
Surely , you could refer to varied format in this case: Convert a Unix timestamp to time in JavaScript
Hope it helps you.
来源:https://stackoverflow.com/questions/51158369/convert-timescript-to-date-in-azure-cosmosdb-sql-query