AddYear method doesn't work as excpected [duplicate] - c#

This question already has an answer here:
Add timespan to another timespan does not work [duplicate]
(1 answer)
Closed 8 years ago.
I need to add years to a DateTime variable, but for some reason this function doesn't affect at all..
This is my code:
DateTime dtCmp = new DateTime(calendarBirthDay.Value.Year, calendarBirthDay.Value.Month, calendarBirthDay.Value.Day);
dtCmp.AddYears(DateTime.Now.Year - calendarBirthDay.Value.Year);
I tried even something trivial likewise :
dtCmp.AddYears(1);
and this is doesn't work yet..

You need to store it back to the variable to reflect the change.
dtCmp = dtCmp.AddYears(1);

AddYears method returns a new instance of DateTime object because DateTime is a struct. You need to store returned value into itself to increase old value:
dtCmp = dtCmp.AddYears(1)

Related

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.

How to get only Folder Creation Date in C# [duplicate]

This question already has answers here:
Getting Date or Time only from a DateTime Object
(7 answers)
Closed 8 years ago.
I know that, Example: DateTime fileCreatedDate = File.GetCreationTime(#"C:\Example\MyTest.txt");will return the date and time of a folder creation but how do I only get the "Date"Example "2014-12-01" or "Decemeber 12,2014" of the folder creation excluding the time?
You can use the DateTime.Date property for that, i.e. fileCreatedDate.Date.

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

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.

Get property info of all properties which values currently equal 'default' [duplicate]

This question already has answers here:
Programmatic equivalent of default(Type)
(14 answers)
Closed 8 years ago.
So, I need to retrieve all properties of an instance which currently have a value that matches the default value of their respective type. Something along the lines of
GetType().GetProperties().Where(x =>
x.GetValue(this).Equals(default(x.PropertyType)));
This obviously doesn't work because it seems 'x' cannot be resolved anymore at this point. What could I do?
The problem is slightly different. You cannot pass a runtime instance of Type to default. Your problem can be simplified to this:
var type = typeof (string);
var defaultValue = default(type); // doesn't work
That doesn't work. Instead, you want to get the default value at run time, which has been answered by this question.

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