简体   繁体   English

将日期转换为特定格式的字符在R中

[英]Convert date to character in particular format In R

I need to map 3-4 different dataframes that have different date formats. 我需要映射具有不同日期格式的3-4种不同的数据帧。 How can we convert date in format: 我们如何以格式转换日期:

YYYY-MM-DD

to character in the format: 到格式的字符:

MMM-YY

Create a date object from the string (skip this if your column is already in the Date format): 从字符串创建日期对象(如果您的列已经是日期格式,请跳过此对象):

original_date <- as.Date("2017-07-19", "%Y-%m-%d")

Then format the original date to the new format: 然后将原始日期格式化为新格式:

format(original_date, "%b-%y")   
# "Jul-17"

The %b indicates the 3-letter abbreviation of the month and %y is the year without the century. %b表示月份的3个字母缩写, %y表示没有世纪的年份。 You can find more of these codes and their meaning here: http://strftime.org/ 您可以在这里找到更多这些代码及其含义: http//strftime.org/

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

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