Solved

format string date

Posted on 2013-02-04
7
375 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now