How to Get the Current Date in the 10 Most Popular Programming Languages
Basher GoaderGetting the current date is a basic but important task in programming, often needed for logs, reports, and user interfaces. Here’s how you can retrieve today’s date in the 10 most popular programming languages. As an alternative, you can always check the current date online at todaysdatenow.com.
1. Python
from datetime import date print(date.today())
2. JavaScript
console.log(new Date().toISOString().slice(0, 10));
3. Java
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
System.out.println(LocalDate.now());
}
}
4. C#
using System;
class Program {
static void Main() {
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd"));
}
}
5. C++
#include <iostream>
#include <ctime>
int main() {
time_t t = time(0);
tm* now = localtime(&t);
printf("%04d-%02d-%02d\n", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
}
6. PHP
echo date("Y-m-d");
7. Go
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Now().Format("2006-01-02"))
}
8. Ruby
puts Date.today
9. TypeScript
console.log(new Date().toISOString().slice(0, 10));
10. Swift
import Foundation let date = Date() let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" print(formatter.string(from: date))