પ્રોગ્રામિંગ ભાષા દ્વારા તારીખ ફોર્મેટ્સ

તારીખો ફોર્મેટ કરવા માટે કોડ ઉદાહરણો

ભાષા પસંદ કરો

ઝડપી સંદર્ભ: YYYY-MM-DD

લોકપ્રિય ભાષાઓમાં YYYY-MM-DD (ISO 8601) તરીકે તારીખ કેવી રીતે ફોર્મેટ કરવી:

💛 JavaScript
const date = new Date(); // Method 1: toISOString (recommended) const formatted = date.toISOString().split('T')[0]; console.log(formatted); // Output: 2025-12-31 // Method 2: Manual formatting const yyyy = date.getFullYear(); const mm = String(date.getMonth() + 1).padStart(2, '0'); const dd = String(date.getDate()).padStart(2, '0'); console.log(`${yyyy}-${mm}-${dd}`); // Output: 2025-12-31
🐍 Python
from datetime import datetime date = datetime.now() formatted = date.strftime('%Y-%m-%d') print(formatted) # Output: 2025-12-31
🐘 PHP
<?php echo date('Y-m-d'); // Output: 2025-12-31
🐬 MySQL
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d') AS formatted_date; -- Output: 2025-12-31

સામાન્ય ફોર્મેટ કોડ્સ

વિવિધ ભાષાઓ વિવિધ ફોર્મેટ કોડ્સ વાપરે છે. અહીં સરખામણી છે:

અર્થ Python/PHP JavaScript SQL
વર્ષ (4 અંક) %Y getFullYear() %Y / YYYY
વર્ષ (2 અંક) %y manual %y / YY
મહિનો (01-12) %m getMonth()+1 %m / MM
મહિના નામ %B toLocaleDateString() %M / Month
દિવસ (01-31) %d getDate() %d / DD

ફોર્મેટ દ્વારા બ્રાઉઝ કરો

તારીખ ફોર્મેટિંગ ટિપ્સ

શ્રેષ્ઠ પ્રથા: ડેટાબેઝ અથવા API માં તારીખો સ્ટોર કરતી વખતે, હંમેશા ISO 8601 ફોર્મેટ (YYYY-MM-DD) વાપરો. તે સ્પષ્ટ છે, યોગ્ય રીતે સોર્ટ થાય છે, અને સાર્વત્રિક રીતે સમજાય છે.
ટાઇમઝોન ટિપ: ટાઇમઝોન તફાવતોથી સાવધ રહો. JavaScript નું toISOString() UTC સમય પરત કરે છે, જ્યારે toLocaleDateString() સ્થાનિક ટાઇમઝોન વાપરે છે.