不同格式的日期之间的差异

时间:2015-04-23 08:53:12

标签: r date format

我想要做的是计算两个日期之间的总天数。如果日期在" yyyy / mm / dd"格式我是这样做的:

EndDate <- "2012/02/01"
StartDate <- "1900/01/01"
DiffBewtweenDates <- data.frame(date=c(EndDate),start=c(StartDate))
DiffBewtweenDates$date_diff <- as.Date(as.character(DiffBewtweenDates$date), format="%Y/%m/%d")- 
as.Date(as.character(DiffBewtweenDates$start), format="%Y/%m/%d")
DiffBewtweenDates

它有效。但是我要求以这种格式至少获得EndDate&#34; FullDayName,FullMethNon的FullMaythName的DayNumber&#34;。这样的事情&#34; 2012年2月1日星期日&#34;

正如我在R手册中所理解的那样,它会...... format="%A, %d of %B of %Y" 但它不起作用,我无法弄清楚原因。

提前感谢任何想法。

2 个答案:

答案 0 :(得分:1)

也许你必须将你的语言环境改为英语:

Sys.setlocale("LC_TIME", "english")
date <- "Sunday, 1 of February of 2012"
lubridate::guess_formats(date, orders = "dmy")
#                 dmy 
# "%A, %d of %B of %Y"

as.Date(date, guess_formats(date, orders = "dmy"))
# [1] "2012-02-01"

无论如何,您可以使用lubridate的guess_formats函数来猜测许多日期字符串的格式。

答案 1 :(得分:1)

只需计算天数差异并获得所需的输出即可

#pragma checksum "C:\Users\User\Documents\Visual Studio 2013\Projects\StripBall\StripBall\StripBall.Shared\UserControls\Modes\Escalade.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "1F4D92492659B77C900C0E6C6C0794B3"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace StripBall.UserControls
{
    partial class Escalade : global::Windows.UI.Xaml.Controls.UserControl, global::Windows.UI.Xaml.Markup.IComponentConnector
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]

        public void Connect(int connectionId, object target)
        {
            this._contentLoaded = true;
        }
    }
}