-3

I wanted to convert date into DD/MM/YYYY/HH:MM:SS format. the date is given by the user.

I am using the code below:

        string fromdate = context.Request.QueryString["fromdate"];
        string todate = context.Request.QueryString["todatedate"];

        DateTime fromDaten = DateTime.MinValue;
        DateTime toDaten = DateTime.MinValue;

        try
        {
            fromDaten = FormatDate(fromdate + " 00:00:00");
            toDaten = FormatDate(todate + " 00:00:00");
        }
        catch (Exception ex) { }

I wanted to convert fromdate and todate into DD/MM/YYYY/HH:MM:SS format. When I try to do this I get 01/01/0001/00:00:00.

in FormatDate i have the code below:

        try
        {

            string dateTimeString = todate;
            dateTimeString = Regex.Replace(dateTimeString, @"[^\u0000-\u007F]", string.Empty);

            string inputFormat = "yyyy-MM-dd HH:mm:ss";
            string outputFormat = "dd-MM-yyyy HH:mm:ss";
            var dateTime = DateTime.ParseExact(dateTimeString, inputFormat, CultureInfo.InvariantCulture);
            string output = dateTime.ToString(outputFormat);
            return Convert.ToDateTime(output);
        }
        catch (Exception ex)
        {
            throw;
        }
mybirthname
  • 17,949
  • 3
  • 31
  • 55
Anupa Sankar
  • 491
  • 2
  • 7
  • 17

4 Answers4

0

That code is a bit of mess, try this:

    string fromdate = context.Request.QueryString["fromdate"];
    string todate = context.Request.QueryString["todatedate"];

    DateTime fromDaten = DateTime.Parse(String.Join("/", fromdate.Split("/".ToCharArray()).Reverse().ToArray()));
    DateTime toDaten = DateTime.Parse(String.Join("/", fromdate.Split("/".ToCharArray()).Reverse().ToArray()));
Gusman
  • 14,905
  • 2
  • 34
  • 50
0

Try this-

        string fromdate = context.Request.QueryString["fromdate"];
        string todate = context.Request.QueryString["todatedate"];

        if(!string.IsNullOrEmpty(fromdate)||!string.IsNullOrEmpty(todate)) /* check  query string value */ 
        {             
         DateTime from=Convert.ToDateTime(fromdate);
         DateTime to=Convert.ToDateTime(todate);
         try
         {
          fromDate=from.ToString("dd/MM/YYYY HH:mm:ss");/*Here you get date time in required format */
          toDate = to.ToString("dd/MM/YYYY HH:mm:ss");  
         }
         catch (Exception ex)
         {
         }
     }
0

You should use TryParse/TryParseExact instead of try/catch which is more expensive and not a best practice.

string fromDate = "2016/10/20/12:13:14";
DateTime fromDaten = DateTime.MinValue;
bool fromParsed = DateTime.TryParseExact(fromDate, "yyyy/MM/dd/HH:mm:ss", 
    new System.Globalization.CultureInfo("en-US"), 
    System.Globalization.DateTimeStyles.None, out fromDaten);

// you may check fromParsed and handle invalid date/time 

I have provided an example for fromDate only. You can create a parse function to deal with all your dates strings.

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
0

Try following

DateTime dt = DateTime.ParseExact(sDate, "MM/dd/yyyy",CultureInfo.InvariantCulture);

Let me know if this works..

A3006
  • 1,051
  • 1
  • 11
  • 28