[javascript] refreshTime object includes year, month, day, hour, minute and second; convert a timestamp into refreshTime object and assign values respectively

To convert a timestamp into an refreshTimeobject containing year, month, day, hour, minute, and second, you can use JavaScript's Dateobject 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 convertTimestampToRefreshTimefunction called which accepts a timestamp as a parameter and uses Datethe object to get the year, month, day, hour, minute and second. We then store these values ​​in refreshTimean object and return that object.

Note that Datethe 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 refreshTimean 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.

Guess you like

Origin blog.csdn.net/gao511147456/article/details/131987905
Recommended