Solved

format string date

Posted on 2013-02-04
7
382 Views
Last Modified: 2013-02-05
if I have a date string like "01022013" which is january 02, 2013
how can I format it to show jan, 02, 13 or january 02, 2013
0
Comment
Question by:sevensnake77
  • 3
  • 3
7 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 293 total points
ID: 38852754
DateTime dt = new DateTime(int.Parse(query.Substring(4, 4)), int.Parse(query.Substring(0, 2)), int.Parse(query.Substring(2, 2)));

If you're using VB

int.Parse = Integer.Parse


Constructor for DateTime is (int year, int month, int day);

From there you can do
dt.ToString("<format>");

where format = http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 38852795
I tried that but kept coming up with todays date not the date I specified.

Dim d = DateTime.Now.ToString("MMM d, yyyy")
Response.Write(String.Format(d, "01022013"))
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 293 total points
ID: 38852805
Response.Write(DateTime.Now.ToString("MMM d, yyyy"))

If you wanted to use the Variable:

Dim d as DateTime = DateTime.Now (not the format);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:sevensnake77
ID: 38852957
give me an example on the current  string i provided up top.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 97 total points
ID: 38852988
C#:

DateTime d;
IFormatProvider provider = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;

if (DateTime.TryParseExact("01022013", "MMddyyyy", provider, DateTimeStyles.None, out d))
{
    string result = d.ToString("MMMM dd, yyyy");
}

Open in new window


VB.NET

Dim d As DateTime
Dim provider As IFormatProvider = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;

If (DateTime.TryParseExact("01022013", "MMddyyyy", provider, DateTimeStyles.None, d))
    Dim result As String= d.ToString("MMMM dd, yyyy");
End If

Open in new window

0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 293 total points
ID: 38853045
dim dateFormat as string = "MMM d, yyyy"
Response.Write(DateTime.Now.ToString(dateFormat ))


Change your dateFormat to match this:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx 

kaufmed has provided one format for you, but knowing how to control the format is the key.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 38855230
tks
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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