Tech Off Thread

6 posts

Question regarding DateTime

Back to Forum: Tech Off
  • rmf919

    If I wanted to send a countdown app (example below, taken from channel 9) to someone in France, how would I convert the date to the proper format?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
    
            static void Main(string[] args)
                 
    
                /* If you want to use this file to tell someone happy birthday,
                 * all you must do is change the "OPEN THIS...", the birthDay value,
                 * and the names, telling who's birthday it is and who it is from.
                 */
    
            {
                // The Information at the top of the Console
                Console.WriteLine("O P E N   T H I S   P R O G R A M   2 0 1 2");
                Console.WriteLine("===========================================");
    
                
    
                //The Variables and Data Types
                DateTime rightNow = DateTime.Now.Date;
                DateTime birthDay = DateTime.Parse("01/28/2012");
                TimeSpan daysLeft = birthDay.Subtract(DateTime.Now);
    
                //If Today is his birthday
                if (rightNow == birthDay)
                {
    
                    Console.WriteLine("Happy Birthday Papa!\n \n From: name");
                    Console.WriteLine("======================================");
                    Console.ReadLine();
                }
                    // If his bithday has already passed
                else if (rightNow > birthDay)
                {
                    Console.WriteLine("Sorry, but this program is valid for this year only.  This program is no longer of  any use, you may delete it.");
                    Console.ReadLine();
                }
                    //If his birthday hasn't quite made it here yet
                else
                {
    
                    Console.WriteLine("Sorry, but you must wait " + daysLeft.Days + " days, then open this file. \n");
                    Console.WriteLine("It is currently: " + DateTime.Now);
                    Console.ReadLine();
                }
            }
        }
    }

     

    I realize this is a bit of a redundant question (why not write as 28/01/2012?). I'm just trying to experiment with the capabilities of DateTime.
  • Dr Herbie

    You could try 

    DateTime.Now.ToString(CultureInfo.CurrentCulture);

    Herbie

     

  • vesuvius

    @rmf919: You should also be using DateTime.TryParse(xx/xx/xxxx);

  • rmf919

    Using the System.Globalization Namespace and appending , CultureInfo.Invariant Culture to line 28 did the trick.

    @vesuvius: I'm relatively new to C#. Would you mind explaining exactly why I'd want to use TryParse in an app like this?

  • vesuvius
  • spivonious

    @vesuvius: I would also forget about the DateTime.Parse and just use the constructor that takes day, month, and year. It makes more sense to me.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.