add the specified number of days to current date in c# [duplicate] - c#

This question already has answers here:
Add 1 week to current date
(3 answers)
Closed 8 years ago.
I want to add number of days with the current date. Help me to find a proper solution. Thank you.
Code:
string s = DateTime.Now.ToString();
I want to add 2 days with the current date.

string s = DateTime.Now.AddDays(2).ToString();
UPDATE
In answer to your comment
string s = DateTime.Now.AddDays(2).ToShortDateString();

DateTime.Now.AddDays(2).ToString();

You can use the AddDays method on the DateTime struct. You can use that method to add or substract any amount of days from the current date, like in this sample.
DateTime added = DateTime.Now.AddDays(2);
string s = added.ToString();
Or even:
DateTime substracted = DateTime.Now.AddDays(-2);
Note that if you are only interested in the date, you could use DateTime.Today rather than DateTime.Now since that will be a little more performant.
A final note on the use of ToString: the output of ToString may differ when the OS running on uses different cultures. If you intend to process this outputted string later on, I suggest to pass in the desired culture, using this overload of ToString.

Related

Convert a formatted date/time string into a DateTime object for comparison? [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 5 years ago.
I'm writing an automation date and I want to extract a string, turn it into a DateTime object, and compare it with the current date and time.
The string in question has this format: 7/28/2017 1:17:29 PM
How can I convert it to a DateTime object to compare with the current time (Basically, my end goal is to verify that it is within a few minutes of the current time)
Use DateTime.Parse() or DateTime.TryParse()
System.Convert can convert to and from many types. For example...
int intElapsedMinutes = (DateTime.Now - Convert.ToDateTime("7/28/2017 1:17:29 PM")).TotalMinutes

C# getting the date [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
convert string to date without time
(3 answers)
Closed 5 years ago.
I'm converting a string to a date format using DateTime.Parse(). My original string consists of the date only but when i use DateTime.Parse() it adds the time to it as well giving me 01/12/2000 00:00:00. I only want the date 01/12/2000. Is there any other way to simply just get the date?
DateTime dt = DateTime.Parse("01/12/2000");
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
DateTime always has a underlying Time fraction, it's just the way you define to show it that makes it look like that. So the tostring function can be used with the given formatting.
If you really want it not to have a time (and i wouldn't know why you'd want that) there are 3th party addons available.

Convert string (ex. 08:00) to datetime c# [duplicate]

This question already has answers here:
Convert UTC/GMT time to local time
(12 answers)
Closed 6 years ago.
I'm using an api that provides time as a string, for example "14:45". Ultimately, I would like to convert that UTC time string to the user's local time zone, but I'm not exactly sure how to do this.
The only solution I could think of is to convert the string to a DateTime and then convert that DateTime to local before pushing that out as a string back in the original format (ex. "14:45" becomes "11:45"). I'm stuck trying to convert the string to a DateTime given that it's not in the typical format. Any suggestions? Thanks!
Alright I was able to figure it out using the following:
string time = "08:00";
var convertedTime = Convert.ToDateTime(time).ToLocalTime().TimeOfDay.ToString().Substring(0,5);

How DateTime.Parse works in case to universal time format [duplicate]

This question already has answers here:
How does DateTime.ToUniversalTime() work?
(4 answers)
Closed 8 years ago.
In my code, I am getting Date value as string (reading from xml doc) where no time zone is specified. Here is the sample code...
string dateStr = "2012-06-23";
DateTime convertedDate = DateTime.Parse(dateStr).ToUniversalTime();
When I check the value of convertedDate, it is "22/06/2012 18:30:00". I want to know how exectly ToUniversalTime() method works in this case and how it concluded dateTime format in this case.
Thanks in advance.
The Parse method sets the Kind property of the date to DateTimeKind.Unspecified as there is no time zone information in the string. The ToUniversalTime method assumes that the time is local and converts it to UTC.
Ref: DateTime.Parse Method
"Generally, the Parse method returns a DateTime object whose Kind
property is DateTimeKind.Unspecified."
Ref: DateTime.ToUniversalTime Method
"Unspecified: The current DateTime object is assumed to be a local time, and the
conversion is performed as if Kind were Local."

Subtract one day from DateTime object [duplicate]

This question already has answers here:
Is there any easy way to increment a DateTime by monthly/yearly/daily units without having to parse it out like crazy?
(4 answers)
Closed 7 years ago.
I want to substrate one day from my date, so that when my date is 30-7-2013 than I want 29-7-2013
Just use the AddDays method, remembering that it doesn't change the value it's called on - it returns a new DateTime value.
DateTime date = ...;
date = date.AddDays(-1);
I am not familiar with windows phone development but you may try to use DateTime.AddDays(-1) method call. According to MSDN this call is supported by windows phone OS.
For example:
var today = DateTime.Now;
var yesterday = today.AddDays(-1);
Alternative version:
DateTime date = ...;
date -= TimeSpan.FromDays(1);

Categories