gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
geet date and time with function wich return like 20240305-22-33pm.json in javascript
- User
geet date and time with function wich return like 20240305-22-33pm.json in javascript
- Assistant
To create a function in JavaScript that returns the date and time in the format
YYYYMMDD-HH-MMam/pm.json, you can use the following code:javascriptfunction getDateTimeFilename() { const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); // Months are 0-based const day = String(now.getDate()).padStart(2, '0'); let hours = now.getHours(); const minutes = String(now.getMinutes()).padStart(2, '0'); const amPm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12 || 12; // Convert to 12-hour format const paddedHours = String(hours).padStart(2, '0'); const formattedDate = `${year}${month}${day}-${paddedHours}-${minutes}${amPm}.json`; return formattedDate; } console.log(getDateTimeFilename());