How to get yesterday date in C#

3 Answers

0 votes
using System;

class Program
{
    static void Main() {
        DateTime yesterday = DateTime.Today.AddDays(-1);

        Console.Write(yesterday);
    }
}




/*
run:

8/9/2023 12:00:00 AM

*/

 



answered Aug 10, 2023 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        DateTime yesterday = DateTime.Now.AddDays(-1);

        Console.Write(yesterday);
    }
}




/*
run:

8/9/2023 5:29:28 PM

*/

 



answered Aug 10, 2023 by avibootz
0 votes
using System;

class Program
{
    static void Main() {
        string yesterday = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");

        Console.Write(yesterday);
    }
}




/*
run:

2023-08-09

*/

 



answered Aug 10, 2023 by avibootz

Related questions

1 answer 190 views
190 views asked Aug 5, 2018 by avibootz
1 answer 173 views
173 views asked Aug 11, 2023 by avibootz
3 answers 185 views
185 views asked Aug 11, 2023 by avibootz
1 answer 133 views
133 views asked Apr 10, 2022 by avibootz
1 answer 186 views
1 answer 207 views
207 views asked Aug 26, 2020 by avibootz
1 answer 136 views
...