By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,992 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,992 IT Pros & Developers. It's quick & easy.

Could someone please explain dates in C# to me?

Lovro Mirnik
P: 8
Hello,

I've been working with Dates today and the entire thing baffles my mind. I'm looking for an explanation of this simple code.
Expand|Select|Wrap|Line Numbers
  1. DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
  2. Console.WriteLine(date1.ToString());
  3.  
  4. // Get date-only portion of date, without its time.
  5. DateTime dateOnly = date1.Date;
  6. // Display date using short date string.
  7. Console.WriteLine(dateOnly.ToString("d"));
  8. // Display date using 24-hour clock.
  9. Console.WriteLine(dateOnly.ToString("g"));
  10. Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));   
  11. // The example displays the following output to the console:
  12. //       6/1/2008 7:47:00 AM
  13. //       6/1/2008
  14. //       6/1/2008 12:00 AM
  15. //       06/01/2008 00:00
  16.  
This code was taken from Microsoft's MSDN Library.

-> First of all, could someone explain the first command? The problematic part is "(2008, 6, 1, 7, 47, 0)". Why is the output arrayed differently?

-> Second, "Console.WriteLine(dateOnly.ToString("d"));" It converts the time into a string, so it can be displayed. But, what does "d" do?

-> Third, "Console.WriteLine(dateOnly.ToString("g"));" It converts the time into a string, so it can be displayed. Again, what does "g" do?

If you'll be this kind... You'll have my full gratitude.

Lovro Mirnik
Feb 24 '11 #1

✓ answered by GaryTexmo

1) What do you mean by "Why is the output arrayed differently?" Those are the parameters to the constructor. http://msdn.microsoft.com/en-us/library/272ba130.aspx

2 & 3) Have a look here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Hope that helps!

Share this Question
Share on Google+
3 Replies


GaryTexmo
Expert 100+
P: 1,501
1) What do you mean by "Why is the output arrayed differently?" Those are the parameters to the constructor. http://msdn.microsoft.com/en-us/library/272ba130.aspx

2 & 3) Have a look here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Hope that helps!
Feb 24 '11 #2

Lovro Mirnik
P: 8
It helped a lot. Actually, I feel bad for not digging deeper into the MSDN Library.

Anyhow, thank you.

Lovro Mirnik
Feb 24 '11 #3

GaryTexmo
Expert 100+
P: 1,501
If it helps, or is possible, I'd recommend developing in Microsoft Visual Studio... at least while you're learning. There are express editions available, free of charge, for exactly this purpose. I unashamedly admit intellisense is responsible for the bulk of my C# knowledge ;)

If you are using Visual Studio already, in addition to intellisense, there's a few more tricks I like to use.

1) Right-click a class and select "Go to definition". For .NET classes, this will take you to the class declaration (sans implementation) for the object, so you can see all the methods available on an object, all nicely documented. This is the same stuff you get out of intellisense, but in a more code-like format. Alternatively you could use the object browser.

2) This is a bit unrelated to your scenario here, but it's helpful to sometimes inspect the code that designer generates. If you do things in designer, all the code goes to "<your file>.designer.cs" and you can open this to see the code the designer writes for you. When I was first learning I found this very helpful for gaining insight on how to use objects.

Hopefully that helps, but if not, good luck either way :)
Feb 24 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.