Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamically formatting a string at runtime

Posted on 2010-01-04
3
Medium Priority
?
411 Views
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.

Chuck
' 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

0
Comment
Question by:chuck620
3 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 1000 total points
ID: 26175664
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) ) ;

Check:
http://msdn.microsoft.com/en-us/library/59bz1f0h.aspx
0
 
LVL 4

Assisted Solution

by:apexpert
apexpert earned 1000 total points
ID: 26178591
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.
0
 

Author Closing Comment

by:chuck620
ID: 31672648
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Screencast - Getting to Know the Pipeline
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

571 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