Solved

Date Convert

Posted on 2011-09-09
7
382 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 62

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 62

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

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 60
VB: Convert 2 dates to specific format 24 48
Winform Module - What is the ASP.Net equiv 2 19
Expression Evaluater 3 24
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

786 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