以“YYYY/MM/DD hh:mm:ss”的格式获取当前日期时间

输出结果

2019/12/09 07:05:25

编程代码示例

Go

package main 
import "fmt"
import "time"
 
func main() {
    jst, _ := time.LoadLocation("Asia/Tokyo")
    fmt.Println(time.Now().In(jst).Format("2006/01/02 15:04:05"))
}

JavaScript

console.log(formatDate(new Date()));

function formatDate(date) {
    var d = new Date(date),
    month = '' + (d.getMonth() + 1),
    day = '' + d.getDate(),
    hour = d.getHours(),
    min = d.getMinutes(),
    sec = d.getSeconds();

    year = d.getFullYear();
    if (month.length < 2)
        month = '0' + month;

    if (day.length < 2)
        day = '0' + day;

    if (hour.length < 2)
        hour = '0' + hour;

    if (min.length < 2)
        min = '0' + min;

    if (sec.length < 2)
        sec = '0' + day;
    return [year, month, day].join('/')+' '+[hour, min, sec].join(':');
}

Perl

use POSIX qw(strftime);
print(strftime("%Y/%m/%d %H:%M:%S", localtime())."\n");

PHP

<?php
date_default_timezone_set('Asia/Tokyo');
echo date('Y/m/d H:i:s');

Python

from datetime import datetime
print( datetime.now().strftime("%Y/%m/%d %H:%M:%S") )

Ruby

puts Time.now.strftime("%Y/%m/%d %H:%M:%S")

Rust

Corgo.toml

[package]
name = "date_time"
version = "0.0.1"

[dependencies]
chrono = "*"

[[bin]]
name = "date_time"
path = "date_time.rs"
程式码
extern crate chrono;
use chrono::{Local, DateTime};

fn main() {
  let now: DateTime<Local> = Local::now();
  println!("{}", now.format("%Y/%m/%d %H:%M:%S"))
}

Shell

DATE=`date '+%Y/%m/%d %H:%M:%S'`;
echo $DATE;