Dynamically formatting a string at runtime

Posted on 2010-01-04
Last Modified: 2013-12-17
I have an application that displays data based on values in a table, so for instance I may have a date field in a particular table that needs to be formatted in a certain way (YYYY-MM-DD, etc).  I have all the layouts and formats in a table but I need to be able to dynamically apply them at run time.  So here's an example:

The data that I'm trying to display is 08/04/09 but I need to format it as the user sees fit, so it could be 2009-08-04, or 04-08-2009, or 04-08-09, etc.  I have the code I need to do the formatting but cannot figure out how to apply it at runtime.

I've searched online and have only found how to apply a format to currency, numbers, date time.  Nothing on how to manipulate a string using String.Format().

This is just an example of one particular field, the formatting can occur on any field at any time and it could be any format.  So currency, or date time, number, left(), right(), etc...)  So i need to be able to apply any of those formats at runtime.

I've attached a code snippet below so you can get an idea of what I'm trying to do.

I know the syntax is VB.Net, but a C# response would be fine as well.
Thanks in advance.

' Original value.

Dim tmp As String = "08/14/09"

' New formatted value.

Dim tmp1 As String

' This data would be retreived out of a database table.

' Trying to format the data as YYYY-MM-DD

Dim fmt As String = """20"" & tmp_input.Substring(7,2) & ""-"" & tmp_input.Substring(1,2) & ""-"" & tmp_input.Substring(4,2)"

' Save the results of the original data with the format applied.

tmp1 = String.Format(fmt, tmp)

Open in new window

Question by:chuck620
    LVL 35

    Accepted Solution

    This C# code will do:
    String.Format("20{0}-{1}-{2}",tmp_input.Substring(7,2),tmp_input.Substring(1,2) , tmp_input.Substring(4,2) ) ;

    LVL 4

    Assisted Solution

    why are you not using this in your query---

    select convert(varchar(20), getdate() , 102) as currentdate
    this return u YYYY/MM/DD format.


    select convert(varchar(20), getdate() , 104) as currentdate
    this return u DD/MM/YYYY format.


    an if you want to change date after retrive data then
    manage this as par requirement
    String.Format("20{0}-{1}-{2}",tmp_input.Substring(7,2),tmp_input.Substring(1,2) , tmp_input.Substring(4,2)  

    above is return YYYY/MM/DD formet you can manage this also.

    Author Closing Comment

    apexpert, the data is not coming from a database, it's coming from a file so that's why I need to do it in code.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now