在R中格式化日期,不带前导零

编程入门 行业动态 更新时间:2024-10-28 14:32:41
本文介绍了在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中格式化日期,不带前导零

本文发布于:2023-10-26 12:38:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前导   不带   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!