Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to replace only Year of a DateTime variable

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.54k
    Comment on it

    How to replace Year of a DateTime variable with the Year of another DateTime variable in C#

    While working in a project, I got stuck in the following issue :-

    I have to compare the year of OldDateTime variable with NewDateTime variable. But if they both are not same I have to replace the year of OldDateTime with the year of NewDateTime.

    The solution which I got for this problem is as follows:-

    DateTime OldDateTime ;
    DateTime NewDateTime;
    
    OldDateTime = (NewDateTime.Year == OldDateTime .Year) ? OldDateTime  : new DateTime(NewDateTime.Year, OldDateTime .Month, OldDateTime .Day, OldDateTime .Hour, OldDateTime .Minute, OldDateTime .Second);
    

    Hope it helps.....

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: