logo
down
shadow

Is there a discontinuity over year end in DateTime


Is there a discontinuity over year end in DateTime

By : Alex Yuan
Date : November 22 2020, 10:31 AM
should help you out I was hoping to write down a countdown timer for a New Year's party tomorrow, but I seem to have encountered an issue with subtracting two DateTimes from eachother. , You're not constructing the dates you think you are. These lines:
code :
var later = new DateTime(2015-01-01);
var now = new DateTime(2014-12-31);
var later = new DateTime(2013);
var now = new DateTime(1972);
var later = new DateTime(2015, 01, 01);
var now = new DateTime(2014, 12, 31);


Share : facebook icon twitter icon
Wanted: DateTime.TryNew(year, month, day) or DateTime.IsValidDate(year, month, day)

Wanted: DateTime.TryNew(year, month, day) or DateTime.IsValidDate(year, month, day)


By : jasper_daniels
Date : March 29 2020, 07:55 AM
seems to work fine There is not a static function IsValidDate() so you have to write it by yourself, first naive implementation may be:
code :
public static bool IsValidDate(int year, int month, int day)
{
    if (year < DateTime.MinValue.Year || year > DateTime.MaxValue.Year)
        return false;

    if (month < 1 || month > 12)
        return false;

    return day > 0 && day <= DateTime.DaysInMonth(year, month);
}
public static DateTime? TryNew(int year,
                               int month,
                               int day,
                               Calendar calendar)
{
    if (calendar == null)
        calendar = new GregorianCalendar();

    if (year < calendar.MinSupportedDateTime.Year)
        return null;

    if (year > calendar.MaxSupportedDateTime.Year)
        return null;

    // Note that even with this check we can't assert this is a valid
    // month because one year may be "shared" for two eras moreover here
    // we're assuming current era.
    if (month < 1 || month > calendar.GetMonthsInYear(year))
        return null;

    if (day <= 0 || day > DateTime.DaysInMonth(year, month))
        return null;

    // Now, probably, date is valid but there may still be issues
    // about era and missing days because of calendar changes.
    // For all this checks we rely on DateTime implementation.        
    try
    {
        return new DateTime(year, month, day, calendar);
    }
    catch (ArgumentOutOfRangeException)
    {
        return null;
    }
}
return TryNew(year, month, day) ?? DateTime.MinValue;
python pandas extract year from datetime --- df['year'] = df['date'].year is not working

python pandas extract year from datetime --- df['year'] = df['date'].year is not working


By : user3684630
Date : March 29 2020, 07:55 AM
With these it helps If you're running a recent-ish version of pandas then you can use the datetime attribute dt to access the datetime components:
code :
In [6]:

df['date'] = pd.to_datetime(df['date'])
df['year'], df['month'] = df['date'].dt.year, df['date'].dt.month
df
Out[6]:
        date  Count  year  month
0 2010-06-30    525  2010      6
1 2010-07-30    136  2010      7
2 2010-08-31    125  2010      8
3 2010-09-30     84  2010      9
4 2010-10-29   4469  2010     10
In [18]:

df['date'] = pd.to_datetime(df['date'])
df['year'], df['month'] = df['date'].apply(lambda x: x.year), df['date'].apply(lambda x: x.month)
df
Out[18]:
        date  Count  year  month
0 2010-06-30    525  2010      6
1 2010-07-30    136  2010      7
2 2010-08-31    125  2010      8
3 2010-09-30     84  2010      9
4 2010-10-29   4469  2010     10
In [20]:

t="""date   Count
6/30/2010   525
7/30/2010   136
8/31/2010   125
9/30/2010   84
10/29/2010  4469"""
df = pd.read_csv(io.StringIO(t), sep='\s+', parse_dates=[0])
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 2 columns):
date     5 non-null datetime64[ns]
Count    5 non-null int64
dtypes: datetime64[ns](1), int64(1)
memory usage: 120.0 bytes
Why does DateTime's strftime give the wrong year when I subtract days from dates near the end of a year?

Why does DateTime's strftime give the wrong year when I subtract days from dates near the end of a year?


By : hervé
Date : March 29 2020, 07:55 AM
it helps some times The strftime code %G is for the year according to the ISO 8601 week number calendar. Days at the end of December might be in the following ISO year, and days at the beginning of January might be in the previous one. Use %Y instead.
PHP datetime->format : Week of Year and Year result in numbers that don't make sense together

PHP datetime->format : Week of Year and Year result in numbers that don't make sense together


By : Narendra Kumar
Date : March 29 2020, 07:55 AM
To fix this issue You need to use the ISO-8601 week numbering year which is o if you want the year for the ISO-8601 week. From the docs:
code :
$dueDate->format('W , o');
How to access last year values to compare year on year ? datetime index

How to access last year values to compare year on year ? datetime index


By : metaforce
Date : March 29 2020, 07:55 AM
wish of those help IIUC, you need this.
This works only when you have date time as index. What we are doing here is grouping by day & month using the datetime value & even if the dates are between leap-year & a normal-year, this should work.
code :
performance_df['LY_Revenue'] = performance_df.groupby([performance_df.index.month,performance_df.index.day])['Revenue'].shift()
print(performance_df)
                Revenue     LY_Revenue
Date        
2018-01-01  25891.846787    NaN
2018-01-02  25851.615541    NaN
2018-01-03  25037.711900    NaN
2018-01-04  26715.764965    NaN
2018-01-05  23988.356950    NaN
2018-01-06  19029.057983    NaN
2018-01-07  16935.481705    NaN
2018-01-08  22756.072913    NaN
2018-01-09  30385.672829    NaN
2018-01-10  32970.132175    NaN
2018-01-11  31089.167075    NaN
2018-01-12  24262.972415    NaN
2018-01-13  18261.273832    NaN
2018-01-14  18304.754084    NaN
2018-01-15  26297.835665    NaN
2018-01-16  32619.669405    NaN
2018-01-17  35565.262225    NaN
2018-01-18  33229.971940    NaN
2018-01-19  25405.647136    NaN
2018-01-20  19980.890375    NaN
2018-01-21  20487.553161    NaN
2018-01-22  29709.032322    NaN
2018-01-23  38164.493648    NaN
2018-01-24  39050.801147    NaN
2018-01-25  36612.554433    NaN
2018-01-26  28169.782524    NaN
2018-01-27  22086.641618    NaN
2018-01-28  21631.662706    NaN
2018-01-29  28419.945290    NaN
2018-01-30  35644.617364    NaN
...     ...     ...
2019-12-02  2973.892113     28289.697207
2019-12-03  2674.316864     34737.317368
2019-12-04  2460.238549     40574.910348
2019-12-05  2800.034200     40556.066887
2019-12-06  3195.262337     39927.322507
2019-12-07  3107.693557     34634.748383
2019-12-08  2961.140812     27666.467364
2019-12-09  2340.478044     27774.363832
2019-12-10  1931.373925     33950.846875
2019-12-11  1847.123639     39518.061312
2019-12-12  2179.325333     39587.568701
2019-12-13  2438.035383     38832.660311
2019-12-14  2379.865127     32258.462222
2019-12-15  2255.598970     23343.008315
2019-12-16  1870.926018     23914.895775
2019-12-17  1620.608382     28173.094175
2019-12-18  1511.311007     30306.555827
2019-12-19  1685.967616     28284.310392
2019-12-20  2099.849763     24228.754426
2019-12-21  2430.507619     20495.999365
2019-12-22  2701.975519     19302.936445
2019-12-23  2997.630051     21391.090777
2019-12-24  2977.347247     21072.220129
2019-12-25  2893.576704     19770.681250
2019-12-26  3207.467022     22751.205447
2019-12-27  3539.618050     25744.075480
2019-12-28  3534.997476     27119.697589
2019-12-29  3527.721147     28894.626077
2019-12-30  3489.915430     30321.364425
2019-12-31  3287.543337     29665.558703
Related Posts Related Posts :
  • C# correct exception handling
  • "Could not open macro storage" when accessing using file on another machine
  • How to access other directories of hosted server
  • C# Jagged Array check if value exists/true
  • Why can't I type Clone() properly?
  • exception on accessing dictionary from list
  • Getting the immediate response from server without waiting to 200 message
  • Why am I getting exception Directory Is Not empty?
  • Could not load file or assembly 'CefSharp.dll' or one of its dependencies
  • Sending Email By Using C# in unity3D?
  • Correct usage of await async in webapi
  • Program update code issue
  • Marshal.Copy attempted to read or write protected memory At Random Times
  • Restrict Type variable to specific class or subclass
  • Horizontal text alignment in a PdfPCell
  • C# crashing with Form.show() command, ObjectDisposedException - Deeper look / explanation please
  • Will the result of a LINQ query always be guaranteed to be in the correct order?
  • "Could not find default endpoint element that references contract"
  • Umbraco Request.QueryString is null if it's the first time the page is loaded
  • Error inconsistent accessibility method C#
  • How to program Intel Xeon Phi with C#?
  • remove nested element using regular expression
  • Is there a C# alternative to Java's vararg parameters?
  • Clear particular column values in DataTable
  • how to add event handler to programatically created checkboxes
  • Cannot apply indexing with [] to an expression of type 'System.Collections.Specialized.NameValueCollection'
  • Check for key in pre-existing dictionary in case insensitive manner
  • How to remove year from datetime object?
  • Accessing Settings in different ways
  • "This project is empty" error in Sonarqube
  • How to create reusable icon menu in Xamarin
  • Value Cannot be null in Ado.Net connectivity
  • Adding a custom/dynamic attribute when using XSD.exe
  • How to convert object to correct type
  • Automatically sign out from Forms Authentication in ASP.NET when browser is closed
  • Can a WCF service support both Buffered and Streamed transfer modes?
  • Verify a CA Certificate with a public key in C#
  • How to invoke a Web Service that requires the "patch" verb using the C# WebClient wrapper?
  • Proper way a implementing property based on generic type
  • Closing a form that is created in another thread
  • How Can You Bind a List<String> to a StackPanel
  • WPF Application Update Best Practices - Architectural Explanation
  • System.UnauthorizedAccessException in Server.MapPath()
  • Connecting and Using SQL Compact Edition in a WPF application
  • C#: weird ref in constructor to behave like "virtual field"
  • C# XDocument Load with multiple roots
  • How to decide what goes in the Domain or Application Project in a "DDD" solution?
  • How to get/set a property of an interface that is not always implemented
  • Read-only array field in unsafe struct
  • i got "Invalid attempt to call Read when reader is closed" when using sqldatareader how to solve it in a three
  • Why should I encapsulate objects in using if there is garbage collection
  • How to load Word document from byte array
  • Caliburn.Micro and ContextMenu for DataGrid Row
  • Linq "join" with a IList<T> getting "Error Unable to create a constant value.."
  • How to draw red wavy line under words in RichTextBox c# winform
  • HttpPostedFileBase returns Null MVC3
  • Refresh Dropdownlist in webform
  • How to convert serialized byte array back to its text form
  • How to do a loop to check all the variables at the same time for C#?
  • Facebook Sentiment Analysis API
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org