Home > SOA Tips > .NET Developer > The System::DateTime structure in .NET
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

The System::DateTime structure in .NET


Kate Gregory
01.20.2004
Rating: -4.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



At times working with dates in code can be a little tricky. Four years ago the date data types in code caused a panic as the new millennium arrived. It would behoove anyone working with .NET to have a firm grasp on the nuances of working with dates. To that end, we present this excerpt from Kate Gregory's article "The .NET System Namespace", provided courtesy of InformIT. In it Gregory provides a brief overview of the DateTime structure in .NET.


The DateTime structure represents a date or a time, or both. It has a number of useful constructors to create instances using a numeric date and time, or a number of ticks (useful when you're working with older C++ code). Here are some examples:

DateTime defaultdate;
Console::WriteLine(defaultdate.ToLongDateString());
DateTime Sept28(2003,9,28,14,30,0,0);
Console::Write(Sept28.ToShortDateString());
Console::Write(S" ");
Console::WriteLine(Sept28.ToShortTimeString());
DateTime now = DateTime::Now;
Console::WriteLine(now.ToString());

It can be intimidating to remember the parameters to the seven-integer constructor, but it's simple when you realize they go from largest to smallest: year, month, day, hour, minute, second, and millisecond. The millisecond parameter is optional and if you want, you can omit all the time parameters completely.

On September 17, 2003, this code produces the following output:

Monday, January 01, 0001
9/28/2003 2:30 PM
9/17/2003 1:17:41 PM

It matters that DateTime is a structure, not a class, because it is managed data. Managed classes can only be allocated on the heap with new; managed structures can only be allocated on the stack as in these examples. To get the individual parts of a date, use these properties:

  • Day: The day of the month
  • Month: 1 to 12
  • Year: Always four digits
  • Hour: 0 to 23
  • Minute
  • Second
  • DayOfWeek: 0 means Sunday
  • Format: Creates a string based on the time and date, using a format string

The format string passed to Format() is either a single character representing one of a number of "canned" formats, or a custom format string. The most useful canned formats include

  • d: A short date, such as 12/19/00
  • D: A long date, such as Tuesday, December 19, 2000
  • f: A full time and date, such as Tuesday, December 19, 2000 17:49
  • g: A general time and date, such as 12/19/00 17:49
  • s: A sortable time and date, such as 2000-12-19 17:49:03
  • t: A short time, such as 17:49
  • T: A long time, such as 17:49:03

If none of the canned formats has what you need, you can make your own by passing in strings such as "MMMM d, yy" for "December 3, 02" or whatever else you desire. You can find all the format strings in the help installed with Visual Studio.


You can read more about the .NET system namespace over at InformIT.


Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Platforms and Servers
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
Decision time: .NET or J2EE?
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices
Working with PDFs in a .NET environment
Displaying errors with the error provider

.NET Developer
Programming Indigo
DataSets and Web services don't mix
Security in .NET 2.0
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices

Microsoft .NET Web services
Microsoft preps .NET 4.0 - framework improves on REST, MVC, JQuery support
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Microsoft .NET Web services Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts