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! Best Practices: The Integrated Project and Portfolio Management Platform.

    Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! Hacking 101

    Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today.
    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! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Hello World: Monitor a simple business process using WebSphere Business Monitor V6.0.2

    This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product.
    FREE! Go There Now!


    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! The dirty dozen: preventing common application-level hack attacks

    As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications.
    FREE! Go There Now!


    Role of Integrated Requirements Management in Software Delivery

    As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change.
    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!



    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek