?
Solved

Converting a string from one date format to another

Posted on 2007-10-02
10
Medium Priority
?
328 Views
Last Modified: 2013-12-17
I have a string which contains date in yyyymmdd format, eg: 20050415.

I need to convert it to a string in dd-MMM-yyyy format eg: 15-Apr-2007.

What is the best way to do this?
0
Comment
Question by:rajesh_khater
  • 5
  • 3
9 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 19999254
use String.Format
0
 
LVL 35

Expert Comment

by:YZlat
ID: 19999266
try this. just make sure that datefield is of type date

String.Format("{0:dd-MMM-yyyy}", datefield)
0
 
LVL 1

Author Comment

by:rajesh_khater
ID: 19999280
it is not of type date, as I already told.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Expert Comment

by:YZlat
ID: 19999290
string mystr="20050415";

DateTime dt = DateTime.Parse (long.Parse (mystr).ToString());

string mynewstring=String.Format("{0:dd-MMM-yyyy}", dt)
0
 
LVL 1

Author Comment

by:rajesh_khater
ID: 19999372
long.parse will take it as long value 20050415, which is not correct. DateTime.Parse interprets long value as a certain offset from a base date, i think.
0
 
LVL 1

Author Comment

by:rajesh_khater
ID: 19999396
long.Parse(mystr).ToString() is equal to mystr only ! That will not help at all.
0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 800 total points
ID: 19999435
int Year;
                  int Day;
                  int Month;

                  string mystr = "20050415";
                  Year = int.Parse(mystr.Substring(0,4));
                  Month = int.Parse(mystr.Substring(4,2));
                  Day = int.Parse(mystr.Substring(6,2));

                  DateTime dt= new DateTime(Year,Month,Day);

                  
                  string mynewstring=dt.ToString("dd-MMM-yyyy");
0
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 1200 total points
ID: 20000097
String myDateStr = "20050415";
DateTime date = DateTime.ParseExact(myDateStr, "yyyyMMdd", null);
String str = date.ToString("dd-MMM-yyyy");

this will result = 15-Apr-2005
0
 
LVL 35

Expert Comment

by:YZlat
ID: 20139209
my solution works, I tested it
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

850 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