Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert date to a different format

Posted on 2011-05-03
7
Medium Priority
?
461 Views
Last Modified: 2012-05-11
I need to convert a date from mm/dd/yyyy format to: yyyy-mm-dd 00:00:00

strSplitDate = "04/30/2011";
DateTime dteSplitDate =
                DateTime.ParseExact(strSplitDate, ?, System.Globalization.CultureInfo.CurrentCulture);
         
                // dteSplitDate should be 2011-04-30 00:00:00

Whatt is the correct syntax for ParseExact?

Thanks

0
Comment
Question by:Dovberman
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 35516251
"MM/dd/yyyy"
0
 

Author Comment

by:Dovberman
ID: 35516425
DateTime dteSplitDate =
                DateTime.ParseExact(strSplitDate, "MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);

This returns 4/28/2011 12:00:00 AM.

I need it to return 2011-04-28 00:00:00
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35516547
This returns 4/28/2011 12:00:00 AM.

... No. It returns a DateTime object. You are just seeing the default implementation of what ToString() prints. If you need to change that, then in your code, where you want to display it, call ToString() with the optional parameter:
string strSplitDate = "04/30/2011";
DateTime dteSplitDate =                 DateTime.ParseExact(strSplitDate, "MM/dd/yyyy, System.Globalization.CultureInfo.CurrentCulture);

...

dteSplitDate.ToString("yyyy-MM-dd");
// Somewhere else

Open in new window

0
Industry Leaders: 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!

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35516555
In this way:
string strSplitDate = "04/30/2011";
DateTime dteSplitDate = DateTime.MinValue;
System.Globalization.CultureInfo ciEnUs = new System.Globalization.CultureInfo("en-US");
if (DateTime.TryParse(strSplitDate, ciEnUs, System.Globalization.DateTimeStyles.AssumeLocal, out dteSplitDate))
    Console.WriteLine(dteSplitDate.ToString("yyyy-MM-dd hh:mm:ss"));
else
    Console.WriteLine("Cant parse");

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35516556
Use ToString() on your "dteSplitDate" variable to get it into the format you want:

    string strNewFormat = dteSplitDate.ToString("yyyy-MM-dd HH:mm:ss");
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35516591
Correction:
string strSplitDate = "04/30/2011";
DateTime dteSplitDate = DateTime.MinValue;
System.Globalization.CultureInfo ciEnUs = new System.Globalization.CultureInfo("en-US");
if (DateTime.TryParse(strSplitDate, ciEnUs, System.Globalization.DateTimeStyles.AssumeLocal, out dteSplitDate))
    Console.WriteLine(dteSplitDate.ToString("yyyy-MM-dd HH:mm:ss"));
else
    Console.WriteLine("Cant parse");

Open in new window

0
 

Author Comment

by:Dovberman
ID: 35516691
Thanks, this works.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

564 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