To convert a timestamp into an refreshTime
object containing year, month, day, hour, minute, and second, you can use JavaScript's Date
object and its corresponding methods.
Here is a sample code:
function convertTimestampToRefreshTime(timestamp) {
const date = new Date(timestamp);
const refreshTime = {
year: date.getFullYear(),
month: date.getMonth() + 1, // 月份从0开始,需要加1
day: date.getDate(),
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
};
return refreshTime;
}
const timestamp = 1636572000000; // 示例时间戳
const refreshTime = convertTimestampToRefreshTime(timestamp);
console.log(refreshTime);
In the above example, we defined a convertTimestampToRefreshTime
function called which accepts a timestamp as a parameter and uses Date
the object to get the year, month, day, hour, minute and second. We then store these values in refreshTime
an object and return that object.
Note that Date
the object's months start at 0, so you need to add 1 to the month value when storing it.
Running the above code using the example timestamp 1636572000000 will output refreshTime
an object similar to the following:
{
year: 2021,
month: 11,
day: 11,
hour: 0,
minute: 0,
second: 0
}
You can modify and adjust it as needed to suit your specific situation.