Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old February 6th, 2004, 11:04 AM
Rich's Avatar
Rich Rich is offline
Administrator
Developer Shed Admin.
 
Join Date: Sep 2003
Location: Fort Lauderdale, FL
Posts: 151 Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 11 h 37 sec
Reputation Power: 10
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.
----------------------------------------------------

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > General Methods of formatting and Subtracting DateTimes ASP.NET Style


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway