Ruby构造日期对象和计算日期间天数差的问题

Python08

Ruby构造日期对象和计算日期间天数差的问题,第1张

我写了个程序 你看一下 不明白的话联系我 有注释

#获取当前的时间

today = Time.new

puts "当前日期:" + today.strftime("%Y-%m-%d %H:%M:%S")#采用格式化输出

#获取指定日期的时间 如 2010-08-30 20:50:01

day = Time.local(2010, 8 , 30 , 20, 50, 1)

puts "指定日期:" + day.strftime("%Y-%m-%d %H:%M:%S")

#获取指定日期的秒数(就是1970年到指定日期的秒数)

sec1 = day.to_i

#同理,获取今天的秒数

sec2 = today.to_i

#秒数求差,同时折合为天数

num = (sec1 - sec2)/(24*60*60)

puts (day.strftime("%Y-%m-%d %H:%M:%S") + "与" + today.strftime("%Y-%m-%d %H:%M:%S") + "相差" + num.to_s + "天")

Net::HTTP.new方法,返回resp码和实际的data:

require 'net/http'  

h = Net::HTTP.new("www.baidu.com",80)  

resp,data = h.get("/")   

puts resp  

puts data

Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)

是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。

UNIX时间戳的0按照ISO 8601规范为 :1970-01-01T00:00:00Z.

一个小时表示为UNIX时间戳格式为:3600秒;一天表示为UNIX时间戳为86400秒,闰秒不计算。

在大多数的UNIX系统中UNIX时间戳存储为32位,这样会引发2038年问题或Y2038。

Java

string date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(Unix timestamp * 1000))

JavaScript

先 var unixTimestamp = new Date(Unix timestamp * 1000) 然后 commonTime = unixTimestamp.toLocaleString()

Linux

date -d @Unix timestamp

MySQL

from_unixtime(Unix timestamp)

Perl

先 my $time = Unix timestamp 然后 my ($sec, $min, $hour, $day, $month, $year) = (localtime($time))[0,1,2,3,4,5,6]

PHP

date('r', Unix timestamp)

PostgreSQL

SELECT TIMESTAMP WITH TIME ZONE 'epoch' + Unix timestamp) * INTERVAL '1 second'

Python

先import time 然后 time.gmtime(Unix timestamp)

Ruby

Time.at(Unix timestamp)

SQL Server

DATEADD(s, Unix timestamp, '1970-01-01 00:00:00')

VBScript / ASP

DateAdd("s", Unix timestamp, "01/01/1970 00:00:00")