js get time (1) year month day time, time in various formats

In JavaScript, you can use the built-in Dateobject to get the current time and format it into different time formats as needed. The following is sample code to get time, year month day and time in various formats:

// 获取当前时间
let currentTime = new Date();

// 获取年份
let year = currentTime.getFullYear();

// 获取月份(注意月份是从0开始计数的,所以需要加1)
let month = currentTime.getMonth() + 1;

// 获取日期
let day = currentTime.getDate();

// 获取小时
let hours = currentTime.getHours();

// 获取分钟
let minutes = currentTime.getMinutes();

// 获取秒数
let seconds = currentTime.getSeconds();

// 获取毫秒数
let milliseconds = currentTime.getMilliseconds();

// 格式化时间为 YYYY-MM-DD
let formattedDate = `${
      
      year}-${
      
      month.toString().padStart(2, '0')}-${
      
      day.toString().padStart(2, '0')}`;

// 格式化时间为 HH:MM:SS
let formattedTime = `${
      
      hours.toString().padStart(2, '0')}:${
      
      minutes.toString().padStart(2, '0')}:${
      
      seconds.toString().padStart(2, '0')}`;

// 格式化时间为 YYYY-MM-DD HH:MM:SS
let formattedDateTime = `${
      
      formattedDate} ${
      
      formattedTime}`;

// 格式化时间为 MM/DD/YYYY
let formattedDateUS = `${
      
      month.toString().padStart(2, '0')}/${
      
      day.toString().padStart(2, '0')}/${
      
      year}`;

// 格式化时间为 HH:MM AM/PM
let formattedTime12Hour = `${
      
      hours % 12 || 12}:${
      
      minutes.toString().padStart(2, '0')} ${
      
      hours < 12 ? 'AM' : 'PM'}`;

console.log('当前时间:', currentTime);
console.log('年份:', year);
console.log('月份:', month);
console.log('日期:', day);
console.log('小时:', hours);
console.log('分钟:', minutes);
console.log('秒数:', seconds);
console.log('毫秒数:', milliseconds);
console.log('格式化日期:', formattedDate);
console.log('格式化时间:', formattedTime);
console.log('格式化日期时间:', formattedDateTime);
console.log('格式化日期(美国格式):', formattedDateUS);
console.log('格式化12小时制时间:', formattedTime12Hour);

This code will get the current time and use different methods to get the year, month, date, hour, minute, seconds and milliseconds. Then, use different formatting methods to format the time into different formats, including YYYY-MM-DD, HH:MM:SS, YYYY-MM-DD HH:MM:SS, MM/DD/YYYY, and HH:MM AM /PM and other formats. You can select an appropriate format method to obtain different time formats according to your needs.

Get the date of yesterday, tomorrow, get the start and end date of this week, last week, next week
In JavaScript, you can use Dateobjects and some date calculation methods to get the date of yesterday, tomorrow, and the date of this week, last week, next week Start and end dates. Here is sample code:

// 获取昨天的日期
let yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

// 获取明天的日期
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

// 获取本周的开始日期和结束日期
let today = new Date();
let startOfWeek = new Date(today.setDate(today.getDate() - today.getDay()));
let endOfWeek = new Date(today.setDate(today.getDate() - today.getDay() + 6));

// 获取上周的开始日期和结束日期
let startOfLastWeek = new Date(startOfWeek);
startOfLastWeek.setDate(startOfLastWeek.getDate() - 7);
let endOfLastWeek = new Date(endOfWeek);
endOfLastWeek.setDate(endOfLastWeek.getDate() - 7);

// 获取下周的开始日期和结束日期
let startOfNextWeek = new Date(startOfWeek);
startOfNextWeek.setDate(startOfNextWeek.getDate() + 7);
let endOfNextWeek = new Date(endOfWeek);
endOfNextWeek.setDate(endOfNextWeek.getDate() + 7);

console.log('昨天的日期:', yesterday);
console.log('明天的日期:', tomorrow);
console.log('本周的开始日期:', startOfWeek);
console.log('本周的结束日期:', endOfWeek);
console.log('上周的开始日期:', startOfLastWeek);
console.log('上周的结束日期:', endOfLastWeek);
console.log('下周的开始日期:', startOfNextWeek);
console.log('下周的结束日期:', endOfNextWeek);

This code uses Datethe object and some date calculation methods to get yesterday's and tomorrow's dates. Then, use some date calculations to get the start and end dates of the week. Next, get last week's start and end dates, and next week's start and end dates by adjusting the week's start and end dates. Finally, print out the obtained date. You can use these dates for further processing and display as needed.

Overall package

When you need to frequently get yesterday's and tomorrow's dates, as well as the start and end dates of this week, last week, and next week, you can encapsulate a function for easy calling. Here is an example of a packaged scenario:

function getDateRange() {
    
    
  let today = new Date();
  let yesterday = new Date();
  yesterday.setDate(yesterday.getDate() - 1);
  let tomorrow = new Date();
  tomorrow.setDate(tomorrow.getDate() + 1);
  let startOfWeek = new Date(today.setDate(today.getDate() - today.getDay()));
  let endOfWeek = new Date(today.setDate(today.getDate() - today.getDay() + 6));
  let startOfLastWeek = new Date(startOfWeek);
  startOfLastWeek.setDate(startOfLastWeek.getDate() - 7);
  let endOfLastWeek = new Date(endOfWeek);
  endOfLastWeek.setDate(endOfLastWeek.getDate() - 7);
  let startOfNextWeek = new Date(startOfWeek);
  startOfNextWeek.setDate(startOfNextWeek.getDate() + 7);
  let endOfNextWeek = new Date(endOfWeek);
  endOfNextWeek.setDate(endOfNextWeek.getDate() + 7);

  return {
    
    
    yesterday,
    tomorrow,
    startOfWeek,
    endOfWeek,
    startOfLastWeek,
    endOfLastWeek,
    startOfNextWeek,
    endOfNextWeek,
  };
}

// 调用函数获取日期范围
let dateRange = getDateRange();

console.log('昨天的日期:', dateRange.yesterday);
console.log('明天的日期:', dateRange.tomorrow);
console.log('本周的开始日期:', dateRange.startOfWeek);
console.log('本周的结束日期:', dateRange.endOfWeek);
console.log('上周的开始日期:', dateRange.startOfLastWeek);
console.log('上周的结束日期:', dateRange.endOfLastWeek);
console.log('下周的开始日期:', dateRange.startOfNextWeek);
console.log('下周的结束日期:', dateRange.endOfNextWeek);

This wrapped schema defines a getDateRangefunction called that returns an object containing yesterday's, tomorrow's dates, and the start and end dates of this week, last week, and next week. Then, getDateRangeget the date range by calling the function, and print out the result. You can integrate this scheme into your project as needed to get date ranges easily.

Advantages of the encapsulation scheme:

  1. Code reuse: The encapsulation scheme can encapsulate repeated code, making the code more concise and maintainable.
  2. Easy to call: The encapsulation scheme can encapsulate complex operations into a function, which is convenient to call and use.
  3. Improves readability: The encapsulation scheme can give a meaningful name to the function, making the code more readable and understandable.

Disadvantages of the encapsulation scheme:

  1. Restrictions: The packaging solution may not be able to meet all usage scenarios, because it is packaged based on specific needs.
  2. Scalability: Encapsulation schemes can be difficult to scale, especially when requirements change and the encapsulation scheme may need to be modified.

Usage Scenarios:
The scheme of encapsulating date ranges is suitable for the following scenarios:

  1. It is necessary to frequently obtain the date of yesterday and tomorrow, as well as the start and end dates of this week, last week, and next week.
  2. Need to use the same date range in multiple places in the project.
  3. It is necessary to encapsulate the acquisition logic of the date range into a reusable function.

In general, the encapsulation scheme is suitable for scenarios that require code reuse and simplification, especially when an operation needs to be used in multiple places. However, when using the encapsulation scheme, it is necessary to pay attention to its applicability and scalability to ensure that the scheme can meet the current needs and can be easily expanded and modified.

Guess you like

Origin blog.csdn.net/ACCPluzhiqi/article/details/132154237