Regex between two times [closed] - c#

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);
}

Related

How to extract date from a string in C#? [closed]

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

DateTime to int [closed]

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.

Check Date Equals 1st Date of Month C# [closed]

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);
}

addition of two month and year in asp.net with c# [closed]

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);

How can I compare Two Dates using C#? [closed]

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
}

Categories