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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1250
  • Last Modified:

How to convert Datetime in the format dd/MM/yyyy using Convert.ToDatetime in C

I have a string in the format dd/MM/yyyy and i am doing Convert.ToDateTime in C#.Net to convert it to DateTime and i am getting the error "threw an exception of type 'System.FormatException'      System.DateTime {System.FormatException}

Can anyone please help.
Thanks
0
MPakeezah
Asked:
MPakeezah
4 Solutions
 
p_davisCommented:
it might be better to try DateTime.Parse or TryParse
0
 
MrPhenixCommented:
Do you have the code?
It is easier to help if I could see what you did that threw that error
0
 
GiftsonDJohnCommented:
Hi

Use like this.

        DateTimeFormatInfo finfo = new DateTimeFormatInfo();
        finfo.ShortDatePattern = "dd/MM/yyyy";
        Convert.ToDateTime("31/01/2009", finfo);
0
 
Anurag ThakurTechnical ManagerCommented:
use datetime.tryparse
http://msdn.microsoft.com/en-us/library/9h21f14e.aspx

also pass in the culture en-GB beacuse you are using dd/MM/yyyy and by default the culture will be MM/dd/yyyy and if you use date as 12/12/2009 it will be ok in both cases and if you use date as 31/12/2009 then casting will give error in en-US (default culture) and will work ok in en-GB culture
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now