Solved

How to add minutes to Date?

Posted on 2009-05-09
8
330 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

688 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