• Based different programming languages and libraries often have their own standards and conventions for formatting dates and times.
» PHP
$date = date('Y-m-d');
echo $date;
» Javascript
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const formattedDate = `${year}-${month}-${day}`;
console.log(formattedDate);
» Java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class HelloWorld {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = date.format(formatter);
System.out.println(formattedDate);
}
}
» Kotlin
import java.time.LocalDate
import java.time.format.DateTimeFormatter
fun main() {
val date = LocalDate.now()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val formattedDate = date.format(formatter)
println(formattedDate)
}
» Python
from datetime import date
today = date.today()
formatted_date = today.strftime("%Y-%m-%d")
print(formatted_date)
» C#
using System;
class Program
{
static void Main()
{
DateTime today = DateTime.Today;
string formattedDate = today.ToString("yyyy-MM-dd");
Console.WriteLine(formattedDate);
}
}
» Swift 5
import Foundation
let today = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let formattedDate = formatter.string(from: today)
print(formattedDate)