Skip to content

时间戳转换

当前时间戳

1738730568

时间戳转日期时间

日期时间转时间戳

时间戳转换代码示例

JavaScript

  // 时间戳转日期
  const timestamp = 1609459200000; // 毫秒
  const date = new Date(timestamp);
  console.log(date.toLocaleString());
  
  // 日期转时间戳
  const now = new Date();
  const timestamp = now.getTime();
  console.log(timestamp);
    
Python

  import time
  from datetime import datetime
  
  # 时间戳转日期
  timestamp = 1609459200  # 秒
  date = datetime.fromtimestamp(timestamp)
  print(date.strftime('%Y-%m-%d %H:%M:%S'))
  
  # 日期转时间戳
  now = datetime.now()
  timestamp = int(now.timestamp())
  print(timestamp)
    
PHP

  <?php
  // 时间戳转日期
  $timestamp = 1609459200;
  echo date('Y-m-d H:i:s', $timestamp);
  
  // 日期转时间戳
  $date = new DateTime();
  echo $date->getTimestamp();
  ?>
    
Java

  import java.time.Instant;
  import java.time.LocalDateTime;
  import java.time.ZoneId;
  
  public class TimestampConverter {
      public static void main(String[] args) {
          // 时间戳转日期
          long timestamp = 1609459200000L; // 毫秒
          LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
          System.out.println(date);
  
          // 日期转时间戳
          long currentTimestamp = System.currentTimeMillis();
          System.out.println(currentTimestamp);
      }
  }