?
Solved

How to add minutes to Date?

Posted on 2009-05-09
8
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
LVL 86

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:
Tony McCreath earned 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

777 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