📜  YYMMDDHHmmss (1)

📅  最后修改于: 2023-12-03 15:21:22.743000             🧑  作者: Mango

YYMMDDHHmmss Format in Programming

YYMMDDHHmmss (Year-Month-Day-Hour-Minute-Second) is a date time format that is widely used in programming. This format is very useful for sorting, parsing, and storing date and time data in an efficient way.

Syntax

The YYMMDDHHmmss format follows a specific syntax that is:

YYMMDDHHmmss

where:

  • YY: represents the last two digits of the year (e.g. 21 for 2021)
  • MM: represents the month (e.g. 01 for January, 02 for February, etc.)
  • DD: represents the day (e.g. 01 for the first day, 31 for the last day)
  • HH: represents the hour (e.g. 00 for midnight, 12 for midday, 23 for the end of the day)
  • mm: represents the minute (e.g. 00 for the start of the hour, 59 for the end of the hour)
  • ss: represents the second (e.g. 00 for the start of the minute, 59 for the end of the minute)
Usage

The YYMMDDHHmmss format can be used for a variety of purposes in programming, such as:

  • Storing date and time data in databases or files
  • Sorting date and time data in ascending or descending order
  • Parsing date and time data from strings
  • Formatting date and time data for display
Examples
Python
import datetime

# get current date time in YYMMDDHHmmss format
current_time = datetime.datetime.now().strftime('%y%m%d%H%M%S')
print(current_time)
# output: 210728122306

# convert YYMMDDHHmmss string to datetime object
dt_object = datetime.datetime.strptime('210728122306', '%y%m%d%H%M%S')
print(dt_object)
# output: 2021-07-28 12:23:06
JavaScript
// get current date time in YYMMDDHHmmss format
const current_time = new Date().toISOString().replace(/[-:.TZ]/g, '').slice(2, 14);
console.log(current_time);
// output: 210728123003

// convert YYMMDDHHmmss string to date object
const date_string = '210728123003';
const year = parseInt('20' + date_string.slice(0, 2));
const month = parseInt(date_string.slice(2, 4)) - 1;
const day = parseInt(date_string.slice(4, 6));
const hour = parseInt(date_string.slice(6, 8));
const minute = parseInt(date_string.slice(8, 10));
const second = parseInt(date_string.slice(10, 12));
const date_obj = new Date(year, month, day, hour, minute, second);
console.log(date_obj);
// output: Wed Jul 28 2021 12:30:03 GMT+0800 (GMT+08:00)
Conclusion

YYMMDDHHmmss is a concise and standardized date time format that can be easily used in various programming applications. It offers an efficient way to store, sort, parse, and display date and time data.