Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
addition of two month and year in asp.net with c#.
if i select any one Month/year: like march/2014 and add(+) 12 month,
so it should be give the February/2014.
In this we can see Loan Period is: 12 (month) and below we can see loan start from month: 07(july/2014) so end of the load should be 06/2015. and the both month are in textbox it means they are string.
DateTime dt = new DateTime(2013, 1, 1);
dt.AddYear(1);
dt.AddMonths(2);
//Date is 2014, 3 (March), 1
Alternatively if you wish to substrat years and months you can use:
dt.AddYear(-1);
dt.AddMonths(-1);
//Date is 2013, 2 (February),1
Here you will not get 02/2015 if you add 12 months in 03/2014. You will get 03/2015 in result as shown below.
var inputString = "march/2014";
DateTime dt = DateTime.ParseExact(inputString, "MMMM/yyyy", CultureInfo.InvariantCulture);
var result = dt.AddMonths(12).ToString("MMMM/yyyy");
Will result in => "march/2015"
It seems that yous should add -1, not 12 months (if you want to get February from March):
String fromDate = "march/2013";
// result == "February/2013"
String result = DateTime
.ParseExact(fromDate, "MMMM/yyyy", CultureInfo.InvariantCulture)
.AddMonths(-1)
.ToString("MMMM/yyyy", CultureInfo.InvariantCulture);
In case that you want to add a year and two months (and so your example is incorrect)
String fromDate = "march/2013";
// result == "May/2014"
String result = DateTime
.ParseExact(fromDate, "MMMM/yyyy", CultureInfo.InvariantCulture)
.AddYears(1)
.AddMonths(2)
.ToString("MMMM/yyyy", CultureInfo.InvariantCulture);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to check a string is combined by year and week number by c# code?
Like "202153",here "53" is the 53th week in 2021,and the week number cannot over 53 in 2021,
so,"202154" is not valid.
Thanks everyone for your suggestions, this is indeed an ambiguous question.I confirmed with my leader that our current system only needs to consider that the number of weeks does not exceed 53.
try this. you can change last line as per logic.
string date1 = "202154";
int year = Convert.ToInt32(date1.Substring(0, 4));
int weekNo = Convert.ToInt32(date1.Substring(4, 2));
//Validate week
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
DateTime date = new DateTime(year, 12, 31);//Get last date of year
Calendar cal = dfi.Calendar;
int weekOfYear = cal.GetWeekOfYear(date, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);//Get last week of the year.
bool answer = weekNo > weekOfYear?false:true;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to get the start date of the current 12-month cycle given a start date and an end date for a period.
for example, the start date is 02/02/2016 and the end date is 18/04/2018.
current start date of the 12-month cycle would be 02/02/2018.
l have something calculating the period from the start date to the end date so it takes care of the cases where start date is after the end date. If the start date is the 29th of February when it's not a leap year it uses the next day (1 march) for that cycle only
You can get the date and month values for start date and the year value for end date and use it to set current start date:
DateTime startDate = (Convert.ToDateTime("02/02/2016"));
var dy = startDate.Day;
var mn = startDate.Month;
DateTime endDate = (Convert.ToDateTime("18/04/2018"));
var yy = endDate.Year;
var currentStartDate = new DateTime(yy, mn, dy);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get report between 2 dates with PersianCalendar and I've already converted the dates. It does not show me the data about last day
This is my code:
public ActionResult FilterDate(DateFormViewModel model)
{
PersianCalendar pc = new PersianCalendar();
var dateFrom = pc.ToDateTime(model.FromYear, model.FromMonth, model.FromDay, 0, 0, 0, 0);
var dateTo = pc.ToDateTime(model.ToYear, model.ToMonth, model.ToDay, 0, 0, 0, 0);
var filter = db.Parts.Where(s => s.CreateDateTime >= dateFrom && s.CreateDateTime <= dateTo).ToList();
return View("Index", filter);
}
You're searching from midnight of the start date to 12 AM of the end date. You want to search from midnight of the start date to 11:59:59 PM of the end date.
You are missing data on the last day, right? (15 mordad)
The reason is that you are looking to the same day. having <= in the query does not help you.
for instance 1395/5/15 01:00:00 is greater than 1395/5/15 (your dateTo) and therefore it will not show up in the results
you have to check it with next day
var filter = db.Parts.Where(s => s.CreateDateTime >= dateFrom && s.CreateDateTime < dateTo.AddDay(1)).ToList();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've been wanting to extract a date in this specific format. month/day/year
string inputText = "Examination held on January 06, 2015 at the University of Bikini Bottom";
I've been thinking about Regex but I have no idea how to implement it.
The following Regex will work for the format in your example, as long as you are not too fussed on validating the date at the extraction stage:
string str = "Examination held on January 06, 2015 at the University of Bikini Bottom";
string regex = #"(January|February|March|April|May|June|July|August|September|October|November|December) *(\d|[0-3]\d), *\d{4}";
var result = Regex.Match(str, regex).Value; // January 06, 2015
for this specific format you can use something like this:
string inputText = "Examination held on January 06, 2015 at the University of Bikini Bottom";
Regex r = new Regex(#"on (\w+ \d+, \d+)");
Console.WriteLine(r.Match(inputText).Groups[1].ToString());
You are going to have to do two things
extract the date sub string
parse it
Is the string always "Examination held on <date> at the"? I hope so
var first = str.Substring(20); // 20 is the length of Examination held on
var idx = first.IndexOf("at the");
var dt = first.Substring(0, idx);
now dt has the date string
Use DateTime.Parse to parse the date - google it
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
User can input any date, month and year like 12/12/2013, 1/1/2014, 7/5/2014, 5/1/2012 in MM\DD\YYYY format.
How to check the date is first date of month ?
If the user entry is not first date of month, I want to modify that entry to 1st date of month. In my Examples, I want
12/12/2013 as 12/1/2013
1/1/2014 as 1/1/2014(No Change)
7/5/2014 as 7/1/2014
5/1/2012 as 5/1/2012(No Change)
Thanks
DateTime date = ... // your original date here...
// Don't bother checking, just create a new date for the 1st.
date = new DateTime(date.Year, date.Month, 1);
UPDATE:
The OP has apparently changed the specs:
DateTime date = ... // your original date here...
if (date.Day != 1)
date = new DateTime(date.Year, date.Month, 1).AddMonths(1);
(let the .AddMonths() method worry about the year rolling over in December...)
IMO, since you have a definite format you expect from users (MM\DD\YYYY) why not do a simple split and dig your hit:
string arbitDate = "4/3/2014";
string UsersFirstDay = arbitDate.Trim().Split(new String[] { "/" }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();//index 1 is the DD part - according to your format
UsersFirstDay = (UsersFirstDay == "1") ? UsersFirstDay : "1";
Pass your date to this function:
public static void ConvertToFirstDate(ref DateTime dt){
TimeSpan ts = dt.Subtract(new DateTime(dt.Year, dt.Month, 1));
dt = dt.AddDays(-ts.Days);
}