Solved

Date Convert

Posted on 2011-09-09
7
388 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 250 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
Stressed Out?

Watch some penguins on the livecam!

 
LVL 50

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 50

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

Technology Partners: 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!

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

724 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