| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import {_decorator, Component, Node} from 'cc';
- const {ccclass, property} = _decorator;
- /**
- * 格式化日期时间
- * @param date Date对象
- * @param format 格式化字符串,例如 'yyyy-MM-dd HH:mm:ss'
- * @returns 格式化后的日期时间字符串
- */
- export const formatDate = (date: Date, format: string = 'yyyy-MM-dd HH:mm:ss'): string => {
- const year = date.getFullYear();
- const month = (date.getMonth() + 1).toString().padStart(2, '0');
- const day = date.getDate().toString().padStart(2, '0');
- const hour = date.getHours().toString().padStart(2, '0');
- const minute = date.getMinutes().toString().padStart(2, '0');
- const second = date.getSeconds().toString().padStart(2, '0');
- // 定义替换规则
- const formatMap: { [key: string]: string } = {
- 'yyyy': year.toString(),
- 'MM': month,
- 'dd': day,
- 'HH': hour,
- 'mm': minute,
- 'ss': second
- };
- // 使用正则表达式替换格式字符串中的占位符
- return format.replace(/yyyy|MM|dd|HH|mm|ss/g, (match) => formatMap[match]);
- };
- @ccclass('TimeUtils')
- export class TimeUtils extends Component {
- start() {
- }
- update(deltaTime: number) {
- }
- }
|