Solved

How to add minutes to Date?

Posted on 2009-05-09
8
324 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

11 Experts available now in Live!

Get 1:1 Help Now