Solved

Date Convert

Posted on 2011-09-09
7
380 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now