工具6 - 有趣的在线工具合集

Unix 时间戳与文本转换 - 工具6

在线时间戳转换工具,可以方便地查看当前时间戳,也可以将时间戳转换为时间字符串、时间字符串转换为时间戳,转换后一键复制结果。

工具6Logo

Unix 时间戳转换

当前 Unix 时间戳

当前时间戳
当前时间

Unix 时间戳 ⇒ 时间字符串

时间戳
字符串

时间字符串 ⇒ Unix 时间戳

字符串
时间戳

Unix 时间戳处理代码示例

JavaScript

// 获取 Unix 时间戳
const timestamp = Math.round(Date.now() / 1000);
// Unix 时间戳转字符串
const timestamp = 1704081600;  // 示例Unix时间戳
const formattedTime = new Date(timestamp * 1000).toISOString().slice(0, 19).replace('T', ' ');
console.log(formattedTime);
// 时间字符串转 Unix 时间戳
const dateString = "2024-01-01T12:00:00";  // 示例日期时间字符串,注意ISO格式
const timestamp = new Date(dateString).getTime() / 1000;
console.log(timestamp);

PHP

// 获取 Unix 时间戳
$timestamp = time();
// Unix 时间戳转字符串
$timestamp = 1704081600;  // 示例Unix时间戳
$formattedTime = date('Y-m-d H:i:s', $timestamp);
echo $formattedTime;
// 时间字符串转 Unix 时间戳
$dateString = "2024-01-01 12:00:00";  // 示例日期时间字符串
$timestamp = strtotime($dateString);    
echo $timestamp;

Python

// 获取 Unix 时间戳
import time; 
timestamp = int(time.time())
// Unix 时间戳转字符串
import time

timestamp = 1704081600  # 示例Unix时间戳
formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
print(formatted_time)
// 时间字符串转 Unix 时间戳
from datetime import datetime

date_string = "2024-01-01 12:00:00"  # 示例日期时间字符串
date_format = "%Y-%m-%d %H:%M:%S"
timestamp = int(datetime.strptime(date_string, date_format).timestamp())
print(timestamp)

Go

// 获取 Unix 时间戳
import "time"; 
timestamp := time.Now().Unix()
// Unix 时间戳转字符串
package main

import (
    "fmt"
    "time"
)

func main() {
    timestamp := int64(1704081600)  // 示例Unix时间戳
    t := time.Unix(timestamp, 0)
    formattedTime := t.Format("2006-01-02 15:04:05")
    fmt.Println(formattedTime)
}
// 时间字符串转 Unix 时间戳
package main

import (
    "fmt"
    "time"
)

func main() {
    dateString := "2024-01-01 12:00:00"  // 示例日期时间字符串
    layout := "2006-01-02 15:04:05"
    t, err := time.Parse(layout, dateString)
    if err != nil {
        panic(err)
    }
    timestamp := t.Unix()
    fmt.Println(timestamp)
}

Java

// 获取 Unix 时间戳
long timestamp = System.currentTimeMillis() / 1000;
// Unix 时间戳转字符串
long timestamp = 1704081600L;  // 示例Unix时间戳
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getDefault());
String formattedTime = sdf.format(new Date(timestamp * 1000));
System.out.println(formattedTime);
// 时间字符串转 Unix 时间戳
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        String dateString = "2024-01-01 12:00:00";  // 示例日期时间字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(dateString);
            long timestamp = date.getTime() / 1000;
            System.out.println(timestamp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

C++

// 获取 Unix 时间戳
#include <chrono>; 
#include <ctime>; 
auto now = std::chrono::system_clock::now(); 
std::time_t timestamp = std::chrono::system_clock::to_time_t(now);
// Unix 时间戳转字符串
#include <iostream>
#include <ctime>
#include <iomanip>
#include <sstream>

int main() {
    time_t timestamp = 1704081600;  // 示例Unix时间戳
    std::tm* localTime = std::localtime(&timestamp);
    std::ostringstream oss;
    oss << std::put_time(localTime, "%Y-%m-%d %H:%M:%S");
    std::string formattedTime = oss.str();
    std::cout << formattedTime << std::endl;
    return 0;
}
// 时间字符串转 Unix 时间戳
#include <iostream>
#include <ctime>
#include <sstream>
#include <iomanip>

int main() {
    std::string dateString = "2024-01-01 12:00:00";  // 示例日期时间字符串
    std::istringstream iss(dateString);
    std::tm tm{};
    iss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
    std::time_t timestamp = std::mktime(&tm);
    std::cout << timestamp << std::endl;
    return 0;
}

C

// 获取 Unix 时间戳
long timestamp = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
// Unix 时间戳转字符串
using System;

class Program {
    static void Main() {
        long timestamp = 1704081600;  // 示例Unix时间戳
        DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp);
        string formattedTime = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        Console.WriteLine(formattedTime);
    }
}
// 时间字符串转 Unix 时间戳
using System;
using System.Globalization;

class Program {
    static void Main() {
        string dateString = "2024-01-01 12:00:00";  // 示例日期时间字符串
        DateTime date = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
        long timestamp = (long)date.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
        Console.WriteLine(timestamp);
    }
}
复制成功