简体   繁体   English

如何将数字转换为正常日期?

[英]How to convert numeric numbers into a normal date?

I have got a netcdf file to extract some variables from it. 我有一个netcdf文件从中提取一些变量。 the provider told me that the time variable (A) is hours since 1900-01-01 00:00:0.0 . 提供者告诉我,时间变量(A)是自1900-01-01 00:00:0.0以来的小时数。 I need to convert this variable to a normal date and write to another txt file. 我需要将此变量转换为正常日期并写入另一个txt文件。

f1 <- open.ncdf("C:\\Users\\data.nc")
# [1] "file C:\\Users\\data.nc has 3 dimensions:"
# [1] "longitude   Size: 2"
# [1] "latitude   Size: 2"
# [1] "time   Size: 2920"
# [1] "file C:\\Users\\data.nc has 5 variables:"
# [1] "short stm[longitude,latitude,time]  Longname:soil textre Missval:-32767"

time: 时间:

A <- get.var.ncdf(nc=f1,varid="time")
head(A)
## [1] 990552 990558 990564 990570 990576 990582

another variable: 另一个变量:

B1 <- get.var.ncdf(nc=f,varid="stm")
write.table(t(rbind(A,B1)),file="output.txt")

I need your help to convert the time variable to a normal date before writing to the text output file. 在写入文本输出文件之前,我需要您的帮助将时间变量转换为正常日期。

Date 日期

If A are hours since "1900-01-01" , just divide them by 24, and pass this date into the origin parameter within the as.Date function, as in 如果A"1900-01-01"以来的小时数,则将它们除以24,并将此日期传递给as.Date函数中的origin参数,如

A <- c(990552, 990558, 990564, 990570, 990576, 990582)
as.Date(A/24, origin = "1900-01-01")
## [1] "2013-01-01" "2013-01-01" "2013-01-01" "2013-01-01" "2013-01-02" "2013-01-02"

Date + time 日期+时间

If you want the hours too, use as.POSIXct instead and multiply by 3600 (You should probably specify your time zone too within tz parameter) 如果你也想要小时,请改用as.POSIXct并乘以3600(你应该在tz参数内指定你的时区)

as.POSIXct(A*3600, origin = "1900-01-01")
## [1] "2013-01-01 02:00:00 IST" "2013-01-01 08:00:00 IST" "2013-01-01 14:00:00 IST" "2013-01-01 20:00:00 IST" "2013-01-02 02:00:00 IST" "2013-01-02 08:00:00 IST"

Time zone 时区

In order to know your time zone, type 为了知道您的时区,请输入

Sys.timezone()

For a full list of possible time zones, type 有关可能时区的完整列表,请键入

OlsonNames()

Adding/Removing hours 添加/删除小时数

Either way, you can remove/add hours by multiplying the number of hours by 3600 and adding/removing from the results, for example 无论哪种方式,您都可以通过将小时数乘以3600并在结果中添加/删除来删除/添加小时数,例如

as.POSIXct(A*3600, origin = "1900-01-01") - 2*3600

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM