Contents
出力結果
2021/03/25
プログラムコード例
Go
package main import "fmt" import "time" func main() { jst, _ := time.LoadLocation("Asia/Tokyo") fmt.Println(time.Now().Add(-2*24*time.Hour).In(jst).Format("2006/01/02")) }
JavaScript
console.log(get_x_days_ago(2)); function get_x_days_ago(x){ var date = new Date(); return formatDate(date.setDate(date.getDate() - x)); } function formatDate(date) { var d = new Date(date), month = '' + (d.getMonth() + 1), day = '' + d.getDate(), year = d.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('/'); }
Perl
my ($s, $mi, $h, $d, $m, $y) = localtime(time - 2 * 24 * 60 * 60); print sprintf("%4d/%02d/%02d", $y + 1900, $m + 1, $d)."\n";
PHP
<?php echo date('Y/m/d', strtotime('2 days ago'))."\n";
Python
from datetime import datetime, timedelta def get_xdays_ago(days_ago): x_days_ago = datetime.now() - timedelta(days=days_ago) return x_days_ago print( get_xdays_ago(2).strftime("%Y/%m/%d") )
Ruby
require "date" d = Date.today - 2 puts d.strftime("%Y/%m/%d")
Rust
Corgo.toml
[package] name = "x_days_ago" version = "0.0.1" [dependencies] chrono = "0.4" [[bin]] name = "x_days_ago" path = "x_days_ago.rs"プログラムコード
extern crate chrono; use chrono::{Duration, Local, DateTime}; fn main() { let date: DateTime<Local> = Local::now() + Duration::days(-2); println!("{}", date.format("%Y/%m/%d")) }
Shell
#/bin/sh date -d "02/01/2000" 2>: 1>:; INVALID=$?; if [ $INVALID == 1 ]; then echo `TZ=+48 date '+%Y/%m/%d'`; else echo `date -d '2 days ago' '+%Y/%m/%d'`; fi