圣诞倒计时

00 Days
:
00 Hrs
:
00 Min
:
00 Sec
live-preview.sh
Initializing...

源代码实现

Python 3 Implementation
import datetime import time import os def print_countdown(): while True: now = datetime.datetime.now() current_year = now.year christmas = datetime.datetime(current_year, 12, 25, 0, 0, 0) if now > christmas: christmas = datetime.datetime(current_year + 1, 12, 25, 0, 0, 0) diff = christmas - now days = diff.days hours, remainder = divmod(diff.seconds, 3600) minutes, seconds = divmod(remainder, 60) os.system('cls' if os.name == 'nt' else 'clear') print("\n" + "="*40) print(" "*9 + "CHRISTMAS COUNTDOWN") print("="*40 + "\n") if diff.days == 0 and now.month == 12 and now.day == 25: print(" "*10 + "Merry Christmas!\n") else: print(f" {days} Days : {hours:02d} Hrs : {minutes:02d} Min : {seconds:02d} Sec\n") print(" "*10 + "Waiting for December 25th.") print("\n" + "-"*40) print("-" * 40 + "\n") time.sleep(1) if __name__ == "__main__": print_countdown()
Node.js Implementation
function printCountdown() { const now = new Date(); let christmas = new Date(now.getFullYear(), 11, 25); if (now > christmas) { christmas = new Date(now.getFullYear() + 1, 11, 25); } const diff = christmas - now; const d = Math.floor(diff / 86400000); const h = Math.floor((diff % 86400000) / 3600000); const m = Math.floor((diff % 3600000) / 60000); const s = Math.floor((diff % 60000) / 1000); const pad = n => String(n).padStart(2, '0'); console.clear(); console.log("\n========================================"); console.log(" CHRISTMAS COUNTDOWN"); console.log("========================================\n"); if (d === 0 && now.getMonth() === 11 && now.getDate() === 25) { console.log(" Merry Christmas!\n"); } else { console.log(` ${d} Days : ${pad(h)} Hrs : ${pad(m)} Min : ${pad(s)} Sec\n`); console.log(" Waiting for December 25th."); } console.log("\n----------------------------------------"); console.log("----------------------------------------\n"); } setInterval(printCountdown, 1000); printCountdown();
Bash Shell Implementation
#!/bin/bash while true; do current_year=$(date +%Y) target="$current_year-12-25 00:00:00" now=$(date +%s) christmas=$(date -d "$target" +%s) if [ $now -gt $christmas ]; then target="$((current_year + 1))-12-25 00:00:00" christmas=$(date -d "$target" +%s) fi diff=$((christmas - now)) d=$((diff / 86400)) h=$(( (diff % 86400) / 3600 )) m=$(( (diff % 3600) / 60 )) s=$((diff % 60)) clear echo -e "\n========================================" echo " CHRISTMAS COUNTDOWN" echo -e "========================================\n" printf " %d Days : %02d Hrs : %02d Min : %02d Sec\n\n" $d $h $m $s echo " Waiting for December 25th." echo -e "\n----------------------------------------" echo -e "----------------------------------------\n" sleep 1 done
Go Implementation
package main import ( "fmt" "os" "os/exec" "runtime" "time" ) func clearScreen() { var cmd *exec.Cmd if runtime.GOOS == "windows" { cmd = exec.Command("cmd", "/c", "cls") } else { cmd = exec.Command("clear") } cmd.Stdout = os.Stdout cmd.Run() } func main() { for { now := time.Now() year := now.Year() christmas := time.Date(year, time.December, 25, 0, 0, 0, 0, now.Location()) if now.After(christmas) { christmas = time.Date(year+1, time.December, 25, 0, 0, 0, 0, now.Location()) } diff := christmas.Sub(now) d := int(diff.Hours() / 24) h := int(diff.Hours()) % 24 m := int(diff.Minutes()) % 60 s := int(diff.Seconds()) % 60 clearScreen() fmt.Println("\n========================================") fmt.Println(" CHRISTMAS COUNTDOWN") fmt.Println("========================================\n") if d == 0 && now.Month() == time.December && now.Day() == 25 { fmt.Println(" Merry Christmas!\n") } else { fmt.Printf(" %d Days : %02d Hrs : %02d Min : %02d Sec\n\n", d, h, m, s) fmt.Println(" Waiting for December 25th.") } fmt.Println("\n----------------------------------------") fmt.Println("----------------------------------------\n") time.Sleep(1 * time.Second) } }
Java Implementation
import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; public class ChristmasCountdown { public static void main(String[] args) throws InterruptedException { while (true) { LocalDateTime now = LocalDateTime.now(); int currentYear = now.getYear(); LocalDateTime christmas = LocalDateTime.of(currentYear, 12, 25, 0, 0); if (now.isAfter(christmas)) { christmas = LocalDateTime.of(currentYear + 1, 12, 25, 0, 0); } long diff = ChronoUnit.SECONDS.between(now, christmas); long d = diff / 86400; long h = (diff % 86400) / 3600; long m = (diff % 3600) / 60; long s = diff % 60; // Clear console (Works in most ANSI supported terminals) System.out.print("\033[H\033[2J"); System.out.flush(); System.out.println("\n========================================"); System.out.println(" CHRISTMAS COUNTDOWN"); System.out.println("========================================\n"); if (d == 0 && now.getMonthValue() == 12 && now.getDayOfMonth() == 25) { System.out.println(" Merry Christmas!\n"); } else { System.out.printf(" %d Days : %02d Hrs : %02d Min : %02d Sec\n\n", d, h, m, s); System.out.println(" Waiting for December 25th."); } System.out.println("\n----------------------------------------"); System.out.println("----------------------------------------\n"); Thread.sleep(1000); } } }
C++ Implementation
#include <iostream> #include <chrono> #include <thread> #include <iomanip> using namespace std; using namespace std::chrono; void clearScreen() { #ifdef _WIN32 system("cls"); #else system("clear"); #endif } int main() { while (true) { auto now = system_clock::now(); time_t now_c = system_clock::to_time_t(now); struct tm* parts = localtime(&now_c); int current_year = parts->tm_year + 1900; struct tm christmas_tm = {0}; christmas_tm.tm_year = current_year - 1900; christmas_tm.tm_mon = 11; // 0-indexed December christmas_tm.tm_mday = 25; time_t christmas_c = mktime(&christmas_tm); if (now_c > christmas_c) { christmas_tm.tm_year += 1; christmas_c = mktime(&christmas_tm); } int diff = difftime(christmas_c, now_c); int d = diff / 86400; int h = (diff % 86400) / 3600; int m = (diff % 3600) / 60; int s = diff % 60; clearScreen(); cout << "\n========================================\n"; cout << " CHRISTMAS COUNTDOWN\n"; cout << "========================================\n\n"; if (d == 0 && parts->tm_mon == 11 && parts->tm_mday == 25) { cout << " Merry Christmas!\n\n"; } else { cout << " " << d << " Days : " << setfill('0') << setw(2) << h << " Hrs : " << setw(2) << m << " Min : " << setw(2) << s << " Sec\n\n"; cout << " Waiting for December 25th.\n"; } cout << "\n----------------------------------------\n"; cout << "----------------------------------------\n"; this_thread::sleep_for(seconds(1)); } return 0; }
C# .NET Implementation
using System; using System.Threading; class Program { static void Main() { while (true) { DateTime now = DateTime.Now; DateTime christmas = new DateTime(now.Year, 12, 25); if (now > christmas) { christmas = new DateTime(now.Year + 1, 12, 25); } TimeSpan diff = christmas - now; Console.Clear(); Console.WriteLine("\n========================================"); Console.WriteLine(" CHRISTMAS COUNTDOWN"); Console.WriteLine("========================================\n"); if (diff.Days == 0 && now.Month == 12 && now.Day == 25) { Console.WriteLine(" Merry Christmas!\n"); } else { Console.WriteLine($" {diff.Days} Days : {diff.Hours:D2} Hrs : {diff.Minutes:D2} Min : {diff.Seconds:D2} Sec\n"); Console.WriteLine(" Waiting for December 25th."); } Console.WriteLine("\n----------------------------------------"); Console.WriteLine("----------------------------------------\n"); Thread.Sleep(1000); } } }
Rust Implementation
// Required in Cargo.toml: chrono = "0.4" use std::thread; use std::time::Duration; use chrono::{Local, Datelike, TimeZone}; fn main() { loop { let now = Local::now(); let mut year = now.year(); let mut christmas = Local.with_ymd_and_hms(year, 12, 25, 0, 0, 0).unwrap(); if now > christmas { year += 1; christmas = Local.with_ymd_and_hms(year, 12, 25, 0, 0, 0).unwrap(); } let diff = christmas.signed_duration_since(now); let d = diff.num_days(); let h = diff.num_hours() % 24; let m = diff.num_minutes() % 60; let s = diff.num_seconds() % 60; // ANSI escape code to clear screen print!("{esc}[2J{esc}[1;1H", esc = 27 as char); println!("\n========================================"); println!(" CHRISTMAS COUNTDOWN"); println!("========================================\n"); if d == 0 && now.month() == 12 && now.day() == 25 { println!(" Merry Christmas!\n"); } else { println!(" {} Days : {:02} Hrs : {:02} Min : {:02} Sec\n", d, h, m, s); println!(" Waiting for December 25th."); } println!("\n----------------------------------------"); println!("----------------------------------------\n"); thread::sleep(Duration::from_secs(1)); } }
Ruby Implementation
require 'date' loop do now = Time.now christmas = Time.new(now.year, 12, 25) if now > christmas christmas = Time.new(now.year + 1, 12, 25) end diff = (christmas - now).to_i d = diff / 86400 h = (diff % 86400) / 3600 m = (diff % 3600) / 60 s = diff % 60 system("clear") || system("cls") puts "\n========================================" puts " CHRISTMAS COUNTDOWN" puts "========================================\n\n" if d == 0 && now.month == 12 && now.day == 25 puts " Merry Christmas!\n\n" else printf(" %d Days : %02d Hrs : %02d Min : %02d Sec\n\n", d, h, m, s) puts " Waiting for December 25th." end puts "\n----------------------------------------" puts "----------------------------------------\n\n" sleep 1 end
PHP Implementation
<?php while (true) { $now = time(); $year = date('Y', $now); $christmas = strtotime("$year-12-25 00:00:00"); if ($now > $christmas) { $christmas = strtotime(($year + 1) . "-12-25 00:00:00"); } $diff = $christmas - $now; $d = floor($diff / 86400); $h = floor(($diff % 86400) / 3600); $m = floor(($diff % 3600) / 60); $s = $diff % 60; system(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? 'cls' : 'clear'); echo "\n========================================\n"; echo " CHRISTMAS COUNTDOWN\n"; echo "========================================\n\n"; if ($d == 0 && date('m-d', $now) === '12-25') { echo " Merry Christmas!\n\n"; } else { printf(" %d Days : %02d Hrs : %02d Min : %02d Sec\n\n", $d, $h, $m, $s); echo " Waiting for December 25th.\n"; } echo "\n----------------------------------------\n"; echo "----------------------------------------\n\n"; sleep(1); }
PowerShell Implementation
while ($true) { $now = Get-Date $year = $now.Year $christmas = Get-Date -Year $year -Month 12 -Day 25 -Hour 0 -Minute 0 -Second 0 if ($now -gt $christmas) { $christmas = Get-Date -Year ($year + 1) -Month 12 -Day 25 -Hour 0 -Minute 0 -Second 0 } $diff = $christmas - $now Clear-Host Write-Host "`n========================================" Write-Host " CHRISTMAS COUNTDOWN" Write-Host "========================================`n" if ($diff.Days -eq 0 -and $now.Month -eq 12 -and $now.Day -eq 25) { Write-Host " Merry Christmas!`n" } else { $h = "{0:D2}" -f $diff.Hours $m = "{0:D2}" -f $diff.Minutes $s = "{0:D2}" -f $diff.Seconds Write-Host " $($diff.Days) Days : $h Hrs : $m Min : $s Sec`n" Write-Host " Waiting for December 25th." } Write-Host "`n----------------------------------------" Write-Host "----------------------------------------`n" Start-Sleep -Seconds 1 }