Parameters
Success Callback Parameters
Code Example
Basic Usage
Time-Sensitive Operations
Preventing Time Manipulation
Related APIs
my.getSystemInfo
Get system information
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Get the current server time.
| Property | Type | Required | Description |
|---|---|---|---|
| success | Function | No | Callback on success |
| fail | Function | No | Callback on failure |
| complete | Function | No | Callback that always executes |
| Property | Type | Description |
|---|---|---|
| time | Number | Server timestamp in milliseconds |
my.getServerTime({
success(res) {
console.log('Server time:', res.time);
console.log('Date:', new Date(res.time));
}
});
Page({
data: {
saleEndTime: 1735689600000, // Sale end timestamp
timeRemaining: null
},
onLoad() {
this.updateCountdown();
setInterval(() => this.updateCountdown(), 1000);
},
updateCountdown() {
my.getServerTime({
success: (res) => {
const remaining = this.data.saleEndTime - res.time;
if (remaining > 0) {
this.setData({
timeRemaining: this.formatTime(remaining)
});
} else {
this.setData({ timeRemaining: 'Sale ended' });
}
}
});
},
formatTime(ms) {
const hours = Math.floor(ms / 3600000);
const minutes = Math.floor((ms % 3600000) / 60000);
const seconds = Math.floor((ms % 60000) / 1000);
return `${hours}h ${minutes}m ${seconds}s`;
}
});
async function validateAction() {
return new Promise((resolve, reject) => {
my.getServerTime({
success(res) {
const serverTime = res.time;
const actionDeadline = 1735689600000;
if (serverTime > actionDeadline) {
reject(new Error('Action deadline has passed'));
} else {
resolve(serverTime);
}
},
fail(err) {
reject(err);
}
});
});
}
// Usage
try {
const time = await validateAction();
// Proceed with time-sensitive action
} catch (err) {
my.showToast({ content: err.message, type: 'fail' });
}