I am trying to set a variable that calculates the date six months prior to today's date.
string exampleDate = (DateTime.TodaysDate) - (00/06/00)
Or something like that... I know the above definitely does not work.
I would be grateful for any ideas.
Thanks
DateTime date = DateTime.Now.AddMonths(-6);
// if you want to convert to string:
string myDate = date.ToString();
Simply:
var sixMonthsPriorToNow = DateTime.Now.AddMonths(-6);
Related
Hi I want user to select dates which is 6 months or higher than the current date but I don't know where should I start.
Please help me fix this. Thank you!
Please check below solution
DateTimePicker dateTimePicker= new DateTimePicker();
dateTimePicker.MinDate = DateTime.Now;
dateTimePicker.MaxDate = DateTime.Now.AddMonths(6);
You can take the min DateTime value. Try like:
var minDate = DateTime.MinValue;
I am trying to convert a string into DateTimeOffset. here is an example of my string 2017/010/23:51:50 2017 represents year 010 represent day of the year and 23:51:50 is time.
I am trying in below way but it returns me 0001-01-01 00:00:00.0000000 +00:00 always no mater the input is.
My code
DateTimeOffset DateTime;
string year = ("2017/010/23:51:50");
DateTimeOffset.TryParse(year, out DateTime);
Any suggestion please?
Update
For simplicity I did not linger my question. My date time I am getting year (2017 it could be 2002, 2001 ) from name of a .txt file and day and time (010/23:51:50 some has offset and some content don't) from the content of that .txt file. So my input is not always same. hope this clarifies
First split the string by / and then use the dayOfTheYear value and the year to obtain the year/month/date. Next split the time parameter and use it to obtain TimeSpan and add it to the previously obtained date. Next, simply parse your newly obtained date to DateTimeOffset. This code should work:
string year = ("2017/010/23:51:50");
var date = year.Split('/');
var timeSpanVal = date[2].ToString().Split(':').Select(x=>Convert.ToInt32(x)).ToList();
TimeSpan ts = new TimeSpan(timeSpanVal[0], timeSpanVal[1], timeSpanVal[2]);
DateTime newDate = new DateTime(Convert.ToInt32(date[0]), 1, 1).AddDays(Convert.ToInt32(date[1]) - 1)+ts;
DateTimeOffset.TryParse(newDate.ToString(), out DateTime);
Looking through the date and time formats, I don't think you can parse the format Year/JulianDay/Time. What you can do is split the string into parts and then add the days to the year
string[] parts = year.Split('/');
DateTime dt = new DateTime(int.Parse(parts[0]), 1, 1);
dt = dt.AddDays(int.Parse(parts[1]) - 1).Add(TimeSpan.Parse(parts[2]));
this is how I would like that to build up such that it has the date today d 2 / 23-2015 and so it goes 30 days ahead of the date, which means it will be d 22.02.2015,
I've tried to do like this:
string datoTid = DateTime.Now.ToString("dd-MM-yyyy");
DateTime equalsDato = datoTid.AddDays(1 * 30);
string slutdato = equalsDato.ToString("dd-MM-yyyy");
This should work:
string slutdato = DateTime.Now.AddDays(1 * 30).ToString("dd-MM-yyyy");
I'm not entirely sure what your asking, I believe your intent is to take the current date and added thirty days. Which would be:
var date = DateTime.Now.AddDays(30);
This would provide a type of DateTime. Then you can create a string representation of the designated DateTime. (In your desired format)
date.ToString("dd-MM-yyyy");
This would provide your modification for the format, while simply applying the designated date ahead.
So I'm trying to figure out what I'm doing wrong with this logic. It seems straightforward and my breakpoints indicate that the evaulation in the 'if' statement is resolving as True, but sum.ppStart et al aren't getting 14 days added to them.
It's probably something simple, but any help would be appreciated.
//Determine the start/end days of each week of the pay period and retrieve a list of those entries
DateTime[] weeks = timeTools.calcPP(0);
DateTime today = DateTime.Now.Date;
if (today > weeks[3])
{
weeks[0].AddDays(14);
weeks[3].AddDays(14);
weeks[4].AddDays(14);
}
sum.ppStart = weeks[0];
sum.ppEnd = weeks[3];
sum.payDate = weeks[4];
AddDays returns a new instance of DateTime, the existing value is not changed, it is an immutable structure. When using the function, capture the result
DateTime myDate = ...
myDate = myDate.AddDays(14);
That is because you're not using the result of the AddDays method. The signature is
public DateTime AddDays(double days)
or so (see link). You need to do this:
weeks[0] = weeks[0].AddDays(14);
You need to assign the values:
if (today > weeks[3])
{
weeks[0] = weeks[0].AddDays(14);
weeks[3] = weeks[3].AddDays(14);
weeks[4] = weeks[4].AddDays(14);
}
I want today's date in mm/dd/yyyy format from a DateTime variable. How to get it?
I need to check this with some date variable so it must be also in date variable format?plz help
Ex: i need to get today date in mm/dd/yyyy format and i already date which is datetime datatype in mm/dd/yyyy format and i have to compare them
You should use DateTime.Today:
DateTime today = DateTime.Today; // As DateTime
string s_today = today.ToString("MM/dd/yyyy"); // As String
Edit: You edited your post to add another question, so here comes my edit to supply at least some sort of answer.
Update While you can use DateTime.Compare() you should use plain comparisson:
if(today < otherdate)
{
// Do something.
}
Alternatively, you can use DateTime-variables to check against other DateTime-variables using the DateTime.Compare() method. Both otpions will work and it comes down to preference and what you want to do with the result.
int result = DateTime.Compare(today, otherdate);
if(result < 0)
MessageBox.Show("Today is earlier than the 'otherdate'");
elseif(result > 0)
MessageBox.Show("Today is later than the 'other date'");
else
MessageBox.Show("Dates are equal...");
string datestring = DateTime.Now.ToString("MM/dd/yyyy");
MSDN say: Custom Date and Time Format Strings
To convert DateTime variable to string in the specified format:
DateTime d = ...;
string s = d.ToString("MM/dd/yyyy");
If you want to compare only date part of DateTime, not time part:
DateTime d1 = DateTime.Parse("10/10/2011");
DateTime d2 = DateTime.Parse("01/01/2011");
if (d1.Date > d2.Date)
{
// do the stuff
}
DateTime.Now.ToString("MM/dd/yyyy");
DateTime.Today.ToString("MM/dd/yyyy");
DateTime.Parse is what you are looking for...