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! | Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
| | | | Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
| | | | Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. FREE! Go There Now!
| | | | Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
| | | | This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. 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!
| | | | In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
| | | | User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |