ASP.NET Code
  Home arrow ASP.NET Code arrow General Methods of formatting and Subtract...
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 15
    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


    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! Calling all CC Power Users – and those that would like to be!

    Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development
    FREE! Go There Now!


    NEW! Applying lean thinking to the governance of software development

    Effective governance for lean development isn’t about command and control. Instead, the focus is on enabling the right behaviors and practices through collaborative and supportive techniques. Hear from Scott Ambler on how it is far more effective to motivate people to do the right thing than it is to force them to do so. Learn how to form a lightweight, collaboration-based framework that reflects the realities of modern IT organizations.
    FREE! Go There Now!


    NEW! Evaluate WebSphere Extended Deployment Compute Grid V6.1

    Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Integrating XML into Your Enterprise Using Data Federation

    XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Test terminal-based applications with Rational Functional Tester

    Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily.
    FREE! Go There Now!


    NEW! Webcast: Accelerating Software Innovation with System z

    Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time.
    FREE! Go There Now!


    NEW! Webcast: IBM Rational Build Forge - Beyond the Build

    The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size.
    FREE! Go There Now!


    NEW! Webcast: What is new in Viper 2 for developers?

    Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications.
    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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT