?
Solved

Date Convert

Posted on 2011-09-09
7
Medium Priority
?
389 Views
Last Modified: 2013-12-17
Hi,

I receive from my request a date in this format:

 "2011-09-08T16:11:56.443+0000"

and  i want to get it in this one
 "T08-09-201116:11:56.443+0000", day -month-year-HH-mm-ss+ms
something like this

Thanks

0
Comment
Question by:abgconsulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Expert Comment

by:Rick
ID: 36510834
If you have a date like this:

   DateTime d = new DateTime(2011, 09, 08, 16, 11, 56, 443, 000);

You could convert it to a string and apply your custom formatting.
For example:

   String s = d.ToString("dd-MM-yyyy hh:mm:ss:ms");
0
 
LVL 1

Expert Comment

by:ananken
ID: 36510875
Try using something in this fashion:

//First parse your input:

DateTime dt;
if(!DateTime.TryParseExact(inputStr, "yyyy=MM=ddTHH:mm:ss.fff+0000",CultueInfo.CurrentUICulture,, DateTimeStyles.None, out dt))
     throw new FormatException("Failed parsing....");

//Now convert
string res = dt.ToString("Tdd-MM-yyyyHH:mm:ss.fff+0000");

Open in new window


Note that I didn't address the +0000 as I didn't quite understand what it stands for...

Enjoy,
ananken.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 36511028
Hi abgconsulting;

The following code snippet will give the wanted format.

string date = "2011-09-08T16:11:56.443";
DateTime dateObj = DateTime.Parse(date);

string formattedDate = dateObj.ToString("TMM-dd-yyyyHH:mm:ss:fff");

Open in new window


Fernando
0
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
LVL 51

Expert Comment

by:Gustav Brock
ID: 36516986
Here is how if your local time is not UTC:

            string request = "2011-09-08T16:11:56.443+0000";
            DateTime requestDate = DateTime.Parse(request, System.Globalization.CultureInfo.CurrentUICulture, System.Globalization.DateTimeStyles.AdjustToUniversal);
            DateTime utcDate = DateTime.SpecifyKind(requestDate, DateTimeKind.Utc);
            string utcText = utcDate.ToString("Tdd-MM-yyyyHH:mm:ss.fff+0000");
            Console.WriteLine(utcText);
            Console.ReadKey();

Open in new window


Note that your timezone indication of 0000 is non-standard; it should read 00:00.
It is, however, parsed correctly and must be taken into account when parsed, but you will have to append it manually to the final string.

/gustav
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 36521613
Located in timezone +02:00, Fernandos method didn't work for me except if the trailing "+0000" was chopped.

/gustav
0
 

Author Comment

by:abgconsulting
ID: 36521722
Hi gustav
Fernando's method  is working well for me, u put this in a function and u call it.
thx,
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 36522134

Not a problem, glad I was able to help.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

762 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