Solved

How to add minutes to Date?

Posted on 2009-05-09
8
326 Views
Last Modified: 2012-05-06
How to add minutes to nullable date.

For some reason the compiler won't let me do this

   if (time != null)
           {
               return time.AddMinutes(min);
           }
public static DateTime AddMinutes(DateTime? time, int min)
        {
           return time.AddMinutes(min);
        }

Open in new window

0
Comment
Question by:frosty1
8 Comments
 
LVL 17

Expert Comment

by:OriNetworks
ID: 24344102
You cant add to a null value.
0
 

Author Comment

by:frosty1
ID: 24344120
I go this working, Sorry probably not the most descriptive question.

In order to get the value out of nullable datetime. I did the following

time.Value.AddMinutes(minutes)




        public static string AddMinutes(DateTime? time, int? min)
        {
          int minutes = (min != null) ? min.Value : 0;
           
            if (time != null)
            {
               return time.Value.AddMinutes(minutes).ToString();
            }

            return string.Empty;
           
        }
0
 
LVL 7

Expert Comment

by:zwei
ID: 24344150
DateTime cannot be null.

All value types have a static member that can be used for an invalid value.

In the case of DateTime use DateTime.MinValue.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24344798
@zwei...see nullable:
http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx

The "?" after the DateTime makes it "nullable"...
0
 
LVL 7

Expert Comment

by:zwei
ID: 24344825
@Idle_Mind: Thanks. You learn something new everyday. =)
0
 
LVL 9

Expert Comment

by:tculler
ID: 24345013
frosty1:
You can't interact with "Nullable" structs as if they are the actual value, though interacting with it is still simple. Instead, you must do the following:

nameOfNullableType.Value.MethodCall();

So, instead, your code should look similar to the following code snippet, if you insist on using a Nullable DateTime. Why, by the way?
public static System.DateTime AddMinutes(System.DateTime? myDateTime, System.Double myMins)
{
    if (myDateTime.HasValue)
    {
        return myDateTime.Value.AddMinutes(myMins);
    }
    else
    {
        // Return something valid for a null date. The following made sense to me.
        // It may also be wise to throw an exception here.
        return System.DateTime.Now.AddMinutes(myMins);
    }
    // OR, my preference, remove the previous lines, and replace it with...
    return (myDateTime.HasValue) ? myDateTime.Value.AddMinutes(myMins) : System.DateTime.Now.AddMinutes(myMins);
}

Open in new window

0
 
LVL 23

Accepted Solution

by:
Tiggerito earned 500 total points
ID: 24347343
You might find the ?? operator useful.

int minutes = (min != null) ? min.Value : 0;

becomes

int minutes = min ?? 0;

http://msdn.microsoft.com/en-us/library/ms173224.aspx
0
 

Author Comment

by:frosty1
ID: 24347942
ah, thanks ?? is handy.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now