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.---------------------------------------------------- | DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Code Articles More By aspfree developerWorks - FREE Tools! | David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
| | | | When you create browser-based applications that display XML data feeds, you often need to code the data-retrieval mechanism and the user interface. Mozilla Firefox provides an infrastructure that frees you from these tasks, so you can concentrate on your application's functionality. Learn how to use Asynchronous JavaScript + XML (Ajax) to download XML data from a Web server, and discover how you can use Extensible Stylesheet Language Transformations (XSLT) to transform it dynamically into Firefox user-interface elements expressed in XML User Interface Language (XUL). You can apply these techniques to any application that uses XML data sources. FREE! Go There Now!
| | | | To create, test, and deploy a Web-based application or Web service rapidly, you need a proven relational database, a standards-compliant Web application server, and a flexible IDE. Ideally, all these software packages are production-tested, simple to obtain, easy to use, and well integrated with one another. This tutorial shows you how to use IBM-backed open source and free software to kick-start your Java Web-based application development. You'll learn exactly where to download such components, install them, and get them working for you today. FREE! Go There Now!
| | | | Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
| | | | Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
| | | | No matter what combination of technologies you prefer to work with as a Web developer, Eclipse is a single integrated development environment (IDE) that can increase your productivity. In Part 2, we'll see how easy it is to develop PHP applications using a different set of Eclipse plug-ins, collectively known as the PHP Development Toolkit (PDT.) FREE! Go There Now!
| | | | In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |