在R中格式化日期,不带前导零 [英] Formatting a date in R without leading zeros
本文介绍了在R中格式化日期,不带前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在日期对象上使用格式
函数,特别是类 POSIXlt
的对象, POSIXct
或日期
,格式为%Y,%m,%d
,以便从这3个字段中的每一个删除前导零?
Is there a way to use the format
function on a date object, specifically an object of class POSIXlt
, POSIXct
, or Date
, with the format %Y, %m, %d
such that leading zeros are stripped from each of those 3 fields?
例如,我想要 format(as.Date (1998-09-02),%Y,%m,%d)
返回 1998,9,2
而不 1998,09,02
。
For example, I would like format(as.Date("1998-09-02"), "%Y, %m, %d")
to return 1998, 9, 2
and not 1998, 09, 02
.
推荐答案
只需删除前导零结束:
> gsub(" 0", " ", format(as.Date("1998-09-02"), "%Y, %m, %d"))
[1] "1998, 9, 2"
使用%e 以获得前导空格而不是前导零。
Use %e to obtain a leading space instead of a leading zero.
这篇关于在R中格式化日期,不带前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文