Solved

Date Convert

Posted on 2011-09-09
7
384 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
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 49

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 49

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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