|
General Methods of formatting and Subtracting DateTimes ASP.NET Style
We recently had a need to figure out how to subtract 7 days from a particular
date and store it into a variable. What this turned into was an five day
marathon of figuring out different ways of formatting dates using ASP.NET.
This might seem a redundant but there are literals thousands of different
ways of manipulating dates inside .NET. Here are my 17 ways I found
along my adventure. Some people would say "find one way and just
use it?".
Not us, we decided to make this an adventure and come
up with a nice reference guide(for us anyway) if the occasion arises to format
dates a certain way. We would suggest for anyone wanting to learn about
.NET. Take about a week and just start out doing something with .NET and
dig into documentation. If you find something new, learn about it and run
with it for awhile. We did!
Sample code section(4 sections)
Contained inside each method are various code samples. We've split them
up to better organize the content!
Method #1 -- Various Ways of Subtracting Time from a Current Date
Method #2 -- General Formatting Techniques
Method #3 -- Using FormatDateTime Function
Method #4 -- Display Specific parts of the
Date(DAY, MONTH, TIME)
Formatting Dates Reference Guide
The default string format options that may be used in Format are as follows:
| d |
Short Date |
| D |
Long Date |
| f |
Full (long date + short time) |
| F |
Full (long date + long time) |
| g |
General (short date + short time) |
| G |
General (short date + long time) |
| m, M |
Month/Day Date |
| r, R |
RFC Standard |
| s |
Sortable without TimeZone info |
| t |
Short Time |
| T |
Long Time |
| u |
Universal with sort able format |
| U |
Universal with Full (long date + long time) format |
| y, Y |
Year/Month Date |
Note: Some systems
will not be able to get dates for 2-digit years of less than
1930.
|
|
|