ASP.NET Code
  Home arrow ASP.NET Code arrow General Methods of formatting and Subtract...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

General Methods of formatting and Subtracting DateTimes ASP.NET Style
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2003-06-01

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    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!

    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(7000) )
      return 
    formatdatetime(NewTime1)
    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(7000) )
      return 
    formatdatetime(NewTime3)
    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! IBM – Taking Web 2.0 to Work

    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!


    NEW! Create dynamic Firefox user interfaces

    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!


    NEW! Kick-start your Java apps

    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!


    NEW! Rational Asset Manager eKit

    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!


    NEW! Rational Build Forge Express eKit

    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!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    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!


    NEW! Trial download: IBM Rational Tester for SOA Quality V7.0.1

    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!


    NEW! Try the IBM SOA Sandbox for Process

    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!


    NEW! Web development with Eclipse Europa, Part 2: The Java EE for Eclipse

    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!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    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!

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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