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 ...
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![bold]Sample Code[/bold]Contained inside each method are various code samples.- Various Ways of Subtracting Time from a Current Date
'Uses the AddDays method to subtract X number of days Public Function Date2() Dim NewTime as DateTime NewTime = DateTime.Now.AddDays(-7) Dim s as string = NewTime return s End Function
'Thanks to Paul Czywczynski for this idea 'This probably (In My opinion) Offers the most flexibility found so far 'Change where the MM/dd/yyyy to whatever 'response.write(System.String.Format("{0:d}",NewTime)) 'would return just the name of the Day Function Date3() Dim NewTime as DateTime = now.addDays(-7) response.write(System.String.Format("{0:MM/dd/yyyy}",NewTime)) End Function
Function Date4() Dim NewTime as DateTime NewTime = now.addDays(-7) return NewTime.ToString() End Function
- General Formatting Techniques
'Uses the toLongTimeString method Public Function Date5() Dim NewTime as DateTime NewTime = Now() return newtime.toLongTimeString() End Function
'Uses the toShortTimeString method Public Function Date6() Dim NewTime as DateTime NewTime = Now() return newtime.toShortTimeString() End Function
'Uses the toLongDateString method Public Function Date7() Dim NewTime as DateTime NewTime = Now() return newtime.toLongDateString() End Function
'Uses the toShortDateString method Public Function Date8() Dim NewTime as DateTime NewTime = Now() return newtime.toShortDatestring() End Function
- Using FormatDateTime Function
'Uses FormatDateTime function General format Function Date9() Dim NewTime as DateTime NewTime = DateTime.Now.Subtract( New TimeSpan(7, 0, 0, 0) ) return formatdatetime(NewTime, 0) End Function
'Uses FormatDateTime function LongDate format Function Date10() Dim NewTime as DateTime NewTime = DateTime.Now.Subtract( New TimeSpan(7, 0, 0, 0) ) return formatdatetime(NewTime, 1) End Function
'Uses FormatDateTime function ShortDate format Function Date11() Dim NewTime as DateTime NewTime = DateTime.Now.Subtract( New TimeSpan(7, 0, 0, 0) ) return formatdatetime(NewTime, 2) End Function
'Uses FormatDateTime function LongTime format Function Date12() Dim NewTime as DateTime NewTime = DateTime.Now.Subtract( New TimeSpan(7, 0, 0, 0) ) return formatdatetime(NewTime, 3) End Function
'Uses FormatDateTime function ShortTime format Function Date13() Dim NewTime as DateTime NewTime = DateTime.Now.Subtract( New TimeSpan(7, 0, 0, 0) ) return formatdatetime(NewTime, 4) End Function
- Display Specific parts of the Date(DAY, MONTH, TIME)
'Bring Back just the name of the Day Function Date14() Dim NewTime as DateTime = now.addDays(-7) dim s as string s = (System.String.Format("{0:dddd}",NewTime)) Return s End Function
'Returns the Integer of what day of week Function Date15() Dim MyDate as DateTime Dim MyWeekDay as Integer MyDate = Now.AddDays(-5) MyWeekDay = Weekday(MyDate) return MyWeekDay End Function
'Returns the Month Integer Function Date16() Dim MyDate as DateTime Dim MyMonth as Integer MyDate = Now.AddDays(-5) MyMonth = Month(MyDate) return MyMonth End Function
'Returns just a formatted string 'This method provides just formatting but 'Very flexible with not a lot of code Function Date17() Dim MyDate as String MyDate = Format(Now(), "yyyy") return MyDate End Function </script>
[bold]Formatting Dates Reference Guide[/bold]----------------------------------------------------[bold]d[/bold]Short Date[bold]D[/bold]Long Date[bold]f[/bold]Full (long date + short time)[bold]F[/bold]Full (long date + long time)[bold]g[/bold]General (short date + short time)[bold]G[/bold]General (short date + long time)[bold]m, M[/bold]Month/Day Date[bold]r, R[/bold]RFC Standard[bold]s[/bold]Sortable without TimeZone info[bold]t[/bold]Short Time[bold]T[/bold]Long Time[bold]u[/bold]Universal with sort able format[bold]U[/bold]Universal with Full (long date + long time) format[bold]y, Y[/bold]Year/Month Date[bold]*Note:[bold] Some systems will not be able to get dates for 2-digit years of less than 1930.----------------------------------------------------
blog comments powered by Disqus