-1

I'm trying to calculate difference between two dates, my code is given below

DateTime daterenew = DateTime.Parse(drow.ItemArray.GetValue(16).ToString()); //18/01/2017
        DateTime datecurrent = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
        renewstatuslbl.Text = ((daterenew - datecurrent).Days) + " Day(s) remains";

But I'm getting an error

"String was not recognized as a valid DateTime."

It's a trap
  • 1,333
  • 17
  • 39
  • you can change this line `DateTime datecurrent = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));` to `DateTime datecurrent = DateTime.Now;` you do not need to parse it again – NtFreX Sep 08 '16 at 07:04
  • and I guess your first line throws the error? – NtFreX Sep 08 '16 at 07:05
  • Possible duplicate of [Datediff getting the date inbetween 2 dates and bind it to a gridview](http://stackoverflow.com/questions/6057377/datediff-getting-the-date-inbetween-2-dates-and-bind-it-to-a-gridview) – VDWWD Sep 08 '16 at 07:06
  • Duplicated question – Sami Sep 08 '16 at 07:06
  • Possible duplicate of [Calculate difference between two dates (number of days)?](http://stackoverflow.com/questions/1607336/calculate-difference-between-two-dates-number-of-days) – Liam Sep 08 '16 at 07:07
  • Possible duplicate of [String was not recognized as a valid DateTime " format dd/MM/yyyy"](http://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy) – Divyang Desai Sep 08 '16 at 07:07
  • 2
    As shown, your problem isn't actually anything to do with calculating the difference between two dates - it's parsing a date in the first place. It's not at all clear to me why your second line isn't just `DateTime today = DateTime.Today;` though. You should avoid string conversions unless they're *really* what your code is trying to achieve, which it isn't here... – Jon Skeet Sep 08 '16 at 07:08
  • you should really try google before asking here – Liam Sep 08 '16 at 07:08

5 Answers5

0

Assuming your drow.ItemArray.GetValue(16).ToString()format is always dd/MM/yyyy. Use ParseExact

        DateTime daterenew = DateTime.ParseExact(drow.ItemArray.GetValue(16).ToString(), "dd/MM/yyyy", null); //18/01/2017
        //DateTime datecurrent = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
        DateTime datecurrent = DateTime.Now;
        renewstatuslbl.Text = ((daterenew - datecurrent).Days) + " Day(s) remains";
Bob
  • 545
  • 3
  • 8
0

I would simplify that:

var daterenew = DateTime.ParseExact("18/01/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture);
var res = ((daterenew - DateTime.Today).Days) + " Day(s) remains";

Note, that DateTime.Now != DateTime.Today.

Liam
  • 27,717
  • 28
  • 128
  • 190
Paweł Dyl
  • 8,888
  • 1
  • 11
  • 27
0

The format of the date , causes the error. 18/01/2017 https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

example I used "yyyy/MM/dd"

//DateTime daterenew = DateTime.Parse("18/01/2017"); //18/01/2017
DateTime daterenew = DateTime.Parse("2017.01.18"); //18/01/2017
DateTime datecurrent = DateTime.Parse(DateTime.Now.ToString("dd/MM/yyyy"));
object renewstatuslbl = ((daterenew - datecurrent).Days) + " Day(s) remains";

thus, I think you can change the string date format first of the value before inserting to drow.ItemArray

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
Von Abanes
  • 706
  • 1
  • 6
  • 19
0

Try something like this

string dateString = drow.ItemArray.GetValue(16).ToString();
DateTime daterenew = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture); 

renewstatuslbl.Text = string.Format("{0} Day(s) remains", (daterenew - DateTime.Now).TotalDays);

Idea is in ParseExact where you can set up format for your date in drow.ItemArray Also look at TotalDays

Anatoli Klamer
  • 2,279
  • 2
  • 17
  • 22
0
enter codenamespace FineCalculation

{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    private void button1_Click(object sender, EventArgs e)
    {
        DateTime date01 = date1.Value;
        DateTime date02 = date2.Value;

        TimeSpan timeSpan = date02 - date01;

        int days = Convert.ToInt16(timeSpan.TotalDays);

        double fine = 0;

        if(days < 30)
        {
            fine = days * 0.5;
        }else if(days > 30 && days < 60)
        {
            fine = days * 0.5 * 0.75;
        }else if(days > 60)
        {
            fine = days * 0.5 * 0.75 * 1;
        }


        MessageBox.Show("For "+ days + " days fine is " + fine.ToString());
    }
}

} here

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31839672) – Valerij Dobler May 28 '22 at 19:44