Solved

How to add minutes to Date?

Posted on 2009-05-09
8
327 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:
Tony McCreath 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

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.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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