Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can I compare Two Dates using C# where one is in my database file and other will be given through a textbox and the date formate is yyyy-mm-dd.
DateTime dt1= DateTime.ParseExact("Yourdate1","yyyy-MM-dd",
CultureInfo.InvariantCulture);
DateTime dt2= DateTime.ParseExact("Yourdate2","yyyy-MM-dd",
CultureInfo.InvariantCulture);
int result = DateTime.Compare(dt1,dt2) ;
if(result == 0)
{
//both dates are same
}
else if(result < 0)
{
//Date1 is lessthan Date2
}
else
{
//Date2 is lessthan Date1
}
Try something like this:
string date = "2014-03-17";
DateTime d1 = DateTime.Parse(date);
DateTime d2 = DateTime.Now.Date;
if (d1.Equals(d2))
{
//Do something
}
Try this:
if(datetime1 == DateTime.ParseExact(txtDateTime.Text,"yyyy-MM-dd",CultureInfo.InvariantCulture))
{
//Code
}
OR
if(datetime1 == Convert.ToDateTime(txtDateTime.Text))
{
//Code
}
Related
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 4 years ago.
Improve this question
I have to edit textbox field and update in Database.
DateTime d2 = Convert.ToDateTime(txteventdate.Text.ToString());
ds = obj.EditEvents(Id, txtname.Text, d2, txtinfo.Text, txtvenue.Text);
if (ds.Tables[0].Rows.Count >= 0)
{
lblid.Text = CommonFunctions.DecryptStr(Request.QueryString["id"].ToString());
txtname.Text = ds.Tables[0].Rows[0]["Name"].ToString();
txtinfo.Text = ds.Tables[0].Rows[0]["info"].ToString();
txtvenue.Text = ds.Tables[0].Rows[0]["venue"].ToString();
txteventdate.Text = ds.Tables[0].Rows[0]["Eventdate"].ToString();
}
The textbox value is "2018-04-19 21:34:00.000".
I tried several times with all possible solutions which I know but it is giving me below error.
String was not recognized as a valid DateTime.
Thanks in Advance.
Below code converts your string to DateTime object. If you want to use the exception safe then use DateTime.TryParseExtract
var date = DateTime.ParseExact(ds.Tables[0].Rows[0]["Eventdate"].ToString(), "yyyy-MM-dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
Please do not convert 'unknown user input' data directly into another type. Try with DateTime.TryParse. Further you could check in advance if even something was entered:
if(string.IsNullOrEmpty(txteventdate.Text))
{
MessageBox.Show("Date is necessary!");
return;
}
if(!DateTime.TryParse(txteventdate.Text, out DateTime parsedDateTime))
{
MessageBox.Show($"Invalid {txteventdate.Text}!");
return;
}
If you want to parse a specific date format you could check for DateTime.TryParseExact. The msdn article has got nice examples at the bottom.
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 6 years ago.
Improve this question
I have a file with a few million lines in them.
Every line starts like this:
2016/04/05 11:20:43.293
I would like a regex (or other option?) to get all the lines that fall between two times. (for example between 11:20 and 11:25)
Also, if it's possible to match one or more words in those lines, that would be helpfull as well. However, perhaps a regex isn't the best way to go then?
You could use DateTime.TryParseExact and File.ReadLines with this LINQ query:
string format = "yyyy/MM/dd HH:mm:ss.fff";
DateTime dt;
var relevantLines = File.ReadLines(path)
.Where(l => l.Length >= format.Length
&& DateTime.TryParseExact(l.Substring(0, format.Length), format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt)
&& dt.TimeOfDay >= start && dt.TimeOfDay <= end);
First you have to use a Regex:
Regex TimePattern = new Regex("\\d{2}:\\d{2}.\\d{3}");
Parse the matches to datetime and check if the time is valid:
foreach (Match M in TimePattern.Matches(FILECONTENT))
{
DateTime Dt = Convert.ToDateTime(M.groups[1]));
//Now you can check if the time "Dt" is between 11:20 and 11:25
}
To compare the time you could use (like described in Is there BETWEEN DateTime in C# just like SQL does?):
public static bool Between(DateTime input, DateTime date1, DateTime date2)
{
return (input > date1 && input < date2);
}
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 7 years ago.
Improve this question
How to covert DateTime to int
DateTime now = DateTime.Now;
int va = now.Year;
// error here
int vd = int.Parse(mYear.Value.ToShortDateString());
result = Convert.ToString(va - vd);
Input string was not in a correct format
Assuming mYear is of Type DateTime - Just do it like you did with now:
int vd = mYear.Year;
If it's a string like "2014", use something like:
DateTime.ParseExact(mYear, "yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
Not sure what Parse is going to do with escape chars.
// The example displays the following output:
// Displaying short date for en-US culture:
// 6/1/2009 (Short Date String)
// 6/1/2009 ('d' standard format specifier)
# ElGavilan commented to the same effect while I was posting.
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);