This question already has answers here:
How to subtract one year from DateTime [closed]
(4 answers)
Closed 8 years ago.
I am trying to find a date (string) that is 5 years from current date in the past:-
DateTime.UtcNow.Date - DateString = 5 years
Can anyone help me formulate this into c# syntax?
var myDate = DateTime.UtcNow;
var newDate = myDate.AddYears(-5);
You can add or subtract a TimeSpan from a DateTime object to get another DateTime object.
var fiveYearsAgo = DateTime.Now - TimeSpan.FromYears(5);
var fiveYearsFromNow = DateTime.Now + TimeSpan.FromYears(5);
Related
This question already has answers here:
Convert Difference between 2 times into Milliseconds?
(11 answers)
Closed 4 years ago.
In Javascript I can use var d = new Date();
var n = d.getTime(); to get the time in milliseconds. Is there any similar method in C#?
Subtracting a DateTime from another DateTime returns a TimeSpan.
DateTime d1 = DateTime.UtcNow;
DateTime d2 = DateTime.UtcNow.AddDays(1);
TimeSpan ts = d2 - d1;
Console.WriteLine(ts.TotalMilliseconds);
// Outputs 86400000
This question already has answers here:
how get yesterday and tomorrow datetime in c#
(9 answers)
Closed 5 years ago.
I have two variables
startdate
enddate
In C#, I want to specify in variables startdate and enddate where the date is equal to yesterday.
For example result I want to achieve:
currentday = '14.09.2017'
startdate = '13.09.2017'
enddate = '13.09.2017'
Try this:
System.DateTime today = System.DateTime.Now; System.DateTime yesterday = today.AddDays(-1);
How about
startdate = DateTime.Now.AddDays(-1)
Doesn't that give you exactly what you want?
You have to use DateTime.Now.Date.AddDays(-1) for yesterday and DateTime.Now.Date.AddDays(1) for tomorrow.
This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
how to convert string year to DateTime format ?
my code :
// click button
string a = "2014";
DateTime b = DateTime.Parse(a);
MessageBox.Show(b.ToString());
You can try something like this
var year = int.Parse("2014");
DateTime dateTime = new DateTime(year, 1, 1);
Console.WriteLine(dateTime.ToString());
This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 8 years ago.
How I can convert string 23:59 to datetime format?
So that I can reduce the current time in it.
Example:
String = 23:59 minus
Datime.Now = 13.8.2014 10:59:55
Time left 12 hours 0 minutes.
i would use ParseExact
TimeSpan tsDifference = DateTime.ParseExact("23:59", "HH:mm", System.Globalization.CultureInfo.InvariantCulture) - DateTime.Now;
use this :
DateTime date = Convert.ToDateTime(string);
System.TimeSpan diff= DateTime.Now.Subtract(date);
diff will contain your deffernce
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calculate previous week's start and end date
I need to get the previous week start date in c#
for eg. today is Jan 9th.
previous week start date is Jan 1th.
I am using the following code
DayOfWeek weekStart = DayOfWeek.Sunday;
DateTime startingDate = DateTime.Today;
while (startingDate.DayOfWeek != weekStart)
startingDate = startingDate.AddDays(-1);
DateTime previousWeekStart = startingDate.AddDays(-7);
Is this the best way in c#
Thanks
As previously stated the duplicate's response will probably do for you
mondayOfLastWeek = DateTime.Now.AddDays( -(int)DateTime.Now.DayOfWeek - 6 );
sundayOfLastWeek = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek)
Try:
DateTime.Today.AddDays(-7);