Làm thế nào để lấy thời gian Unix hiện tại trong các ngôn ngữ lập trình khác nhau?

Learn how to get the current Unix timestamp in different programming languages. Unix timestamp represents the number of seconds since January 1, 1970 UTC and is widely used in software development.

Python
import time
# Get the current timestamp
current_timestamp = int(time.time())
Javascript
// get the current timestamp
var currentTimestamp = Math.floor(Date.now() / 1000);
Java
long currentTimestamp = System.currentTimeMillis() / 1000;
C#
long currentTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
C++
#include <iostream>
#include <chrono>
int main() {
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto epoch = now_ms.time_since_epoch();
auto value = std::chrono::duration_cast<std::chrono::seconds>(epoch);
std::cout << value.count() << std::endl;
return 0;
}
PHP
<?php
$currentTimestamp = time();
echo $currentTimestamp;
?>
TypeScript
const currentTimestamp = Math.floor(Date.now() / 1000);
Swift
let currentTimestamp = Int(Date().timeIntervalSince1970)
Ruby
current_timestamp = Time.now.to_i
Go
package main
import (
"fmt"
"time"
)
func main() {
currentTimestamp := time.Now().Unix()
fmt.Println(currentTimestamp)
}