Forum.
My code reads from an excel file and gets the following string in a variable 'textContainingDate':
"Employee Filter: All Employees; Time Entry Dates: 01/07/2016-01/07/2016; Exceptions: All Exceptions"
What I would like to do is extract the date value from the string. Although there are two dates to be read as a date range, the two dates will always be the same. My thought is, regardless of the scenario, I will be satisfied with the first date I come across.
var dateTime = DateTime.ParseExact(textContainingDate, "MM/dd/yyyy", CultureInfo.CurrentCulture);
I tried using the above statement, which I pieced together from other stackoverflow questions and Googled articles. From what I have read, I believe it fails because it is expecting only a date string.
Any advising on a solution and/or direction towards what text to read to find a solution is appreciated.
You can use Regex.Match to get the first date string you meet. This method returns the first substring that matches a regular expression pattern in an input string.
string stringWithDate = "Employee Filter: All Employees; Time Entry Dates: 01/07/2016-01/07/2016; Exceptions: All Exceptions";
Match match = Regex.Match(stringWithDate, #"\d{2}\/\d{2}\/\d{4}");
string date = match.Value;
if (!string.IsNullOrEmpty(date)) {
var dateTime = DateTime.ParseExact(date, "MM/dd/yyyy", CultureInfo.CurrentCulture);
Console.WriteLine(dateTime.ToString());
}
What the regular expression \d{2}\/\d{2}\/\d{4} does:
var regex = new Regex(#"\d{2}\/\d{2}\/\d{4}");
foreach (Match m in regex.Matches(line))
{
DateTime dt;
if (DateTime.TryParseExact(m.Value, "MM/dd/yyyy", null, DateTimeStyles.None, out dt))
remittanceDateArr[chequeNo - 1] = dt.ToString("MM/dd/yyyy");
rtbExtract.Text = rtbExtract.Text + remittanceDateArr[chequeNo - 1] + "\n";
}
Related
I am trying to format date entered by user.
Dates are provided in the following format:
d/M/yyyy (ie: 1/1/2012, but could be also 12/1/2012 or 1/12/2012)
however I need them converted to:
dd/MM/yyyy (ie: 01/01/2012)
I managed to do it in non-Regex way, like this:
string date = "1/1/2012";
if (date.IndexOf("/") == 1)
{
date = "0" + date;
}
if (date.Substring(4, 1) == "/")
{
date = date.Insert(3, "0");
}
I would really like to know how to do it with Regex.Replace, however, as it would probably be neater.
I tired different variations of the below:
string date = "1/1/2012"
date = Regex.Replace(date, #"\d{1}/", "0$&");
The above will work, but if the date is 12/1/2012, it will also make 102 out of 12. If I add ^ at the beginning of pattern I don't get the second number changed. I also tried combinations with [^|/] at the beginning, but also no luck. So at the moment it is either or.
Use word boundary \b which matches between a word character and a non-word character.
date = Regex.Replace(date, #"\b\d/", "0$&");
OR
date = Regex.Replace(date, #"\b(\d)/", "0$1/");
DEMO
If you're sure of the incoming format, I'd use DateTime.ParseExact instead, then use .ToString() to reformat the date:
DateTime dt = DateTime.ParseExact(input, "d/M/yyyy", CultureInfo.CurrentCulture);
string reformatted = dt.ToString("dd/MM/yyyy");
In a variable of DateTime typeI have this value = {30/07/2014 0:00:00}
I want only the date:
var aux = pedido.ord_cus_deliv_date.ToString().Split(' ')[0];
with it I obtain 30/04/2014 correctly
but when I want to convert in MM/dd/yyyy using:
var aux2 = DateTime.ParseExact(aux, "MM/dd/yyyy", null);
I have this error:
the string is represents one DateTime not admited in the GregorianCalendar
Why I have this error in aux2?
The problem is your locale setting. Calling ToString() without parameters on a date value produces a string with the pattern day,month,year arranged differently between locales. (And I suppose that you get a string arranged with Day,Separator, Month, Separator, Year).
Passing that string to DateTime.ParseExact with a specific pattern (MM/dd/yyyy) requires the string to be in the exact pattern required Month, Day, Year for your example.
You could force the invariant culture in your conversion with
var aux = pedido.ord_cus_deliv_date.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
this produces a string with the pattern required by the subsequent ParseExact mask
However it is not clear why you need these conversions. A date is not a string and you simply keep it as a date and use the conversion only when you need to represent it somewhere (display, print etc...)
Console.WriteLine("Date is:" + pedido.ord_cus_deliv_date.ToString("MM/dd/yyyy"));
When you call below :
var aux = pedido.ord_cus_deliv_date.ToString().Split(' ')[0];
This gives you code "07-30-2014" and not "07/30/2014" and that's generate the error while conversion. So to get "07/30/2014", you have to write
var aux = pedido.ord_cus_deliv_date.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
Below is overall code for you:
DateTime value = DateTime.Parse("30/07/2014 0:00:00"); //your date time value
var aux = value.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
DateTime dt = DateTime.ParseExact(aux, "MM/dd/yyyy", CultureInfo.InvariantCulture);
var aux2 = dt.ToString(CultureInfo.InvariantCulture).Split(' ')[0]);
I hope this will help you
Regards,
Sandeep
Is there any way I don't have to specify the number of digits in day/month/year?
For e.g 1/2/1991
I want a method which satisfies both 1/2/1991,11/3/1990,12/12/1991
I don't know how many digits will be there in either month, year, or days.
My code is
string copy = splittedData[0] + splittedData[1] + splittedData[2];//date+month+year
DateTime datetime = DateTime.ParseExact(copy, "ddMMyyyy", CultureInfo.InvariantCulture);
DateTime dateAndTime = datetime;
The problem is the number of digits in splitted data array are not known to me and thus the above format "ddMMyyyy" give me exception on some cases.
Since you already have the day month and year then just create a date with the three of them like so;
DateTime date = new DateTime(year, month, day);
No parsing is necessary. You already have all the fields you want to create the date, and you dont need to put it into a special format to create a date.
If you are not sure the if the input is valid, then wrap the creation in a try/catch block to catch an ArgumentOutOfRangeException should it should occur.
Since you updated your question with the code you have, you can concatenate date components with a separator like:
string copy = splittedData[0] + "/" + splittedData[1] + "/" + splittedData[2];
Later you can do:
DateTime dt = DateTime.ParseExact(copy, "d/M/yyyy", CultureInfo.InvariantCulture);
I used the format "d/M/yyyy" with single d and M which would account for both single/double digit day/month.
So it will work for dates like:
01/01/2013
1/01/2013
22/09/2013
02/9/2013
DateTime.ParseExact is specifically intended to not allow what you are asking for. DateTime.Parse will allow it, though.
You say you have the 3 parts as separate strings -- if you insert the /'s and parse, it should succeed (InvariantCulture expects the order month-day-year):
string datetimeString = string.Join("/", new[] {month, day, year});
DateTime datetime = DateTime.Parse(datetimeString, CultureInfo.InvariantCulture);
Or you could convert them to integers and construct a DateTime directly:
DateTime datetime = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));
What #n00b said. You've already got the individual components of the date: why are you globbing them back together just so you can call DateTime parsing routines? Just do something like this:
private static DateTime StringToDateTime( string year , string month , string day )
{
int yyyy = int.Parse(year) ;
int mm = int.Parse(month) ;
int dd = int.Parse(day) ;
DateTime dt = new DateTime(yyyy,mm,dd) ;
return dt ;
}
As an added bonus, The above code will probably run faster than DateTime.Parse() or DateTime.ParseExact().
I have a string. The string is like this
Hello I am a Coder, My DOB is 12/09/2011.
I want to extract the date from this sentence. How do I do this using C#? I do not want regular expressions. This was an interview question asked to me recently.
This is my try
string myStr = "Hello 12/3/2013";
DateTime s;
DateTime.TryParse(myStr,out s);
Console.WriteLine(s);
I am getting the output as
01-01-0001 00:00:00
Both C# and JavaScript support regular expressions. You can use this pattern to find that section of the string:
\d{2}/\d{2}/\d{4}
Of course that doesn't ensure that it's a valid date, e.g. 13/88/0000 would match that pattern. You'd then have to parse the string using something like Date.Parse.
However, since you've stated regular expressions are not an option, here's a very crude one-liner:
var input = "Hello I am a Coder, My DOB is 12/09/2011.";
DateTime date = new DateTime();
input.Split().SkipWhile(s => !DateTime.TryParse(s, out date)).Any();
Console.WriteLine(date); // 12/9/2011 12:00:00 AM
Since you don't want to use REGEX, simply split the string on space and then use DateTime.TryParseExact to see if any string gets parsed as DateTime
string str = "Hello I am a Coder, My DOB is 12/09/2011";
string[] array = str.Split();//splits on space
string dateFormat = "M/d/yyyy"; //works with both single digit and double digit
//(day/month) for parsing
//or d/M/yyyy depending your date culture
DateTime tempDateTime;
var result = array.FirstOrDefault(r =>
DateTime.TryParseExact
(r,
dateFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.NoCurrentDateDefault,
out tempDateTime));
Your result would contain the Date string, and your tempDateTime would contain the parsed DateTime.
This question already has answers here:
Date formatting yyyymmdd to yyyy-mm-dd
(9 answers)
Closed 5 years ago.
I have many strings like "20120117" and "20120321". I need to convert it in a new string with this format: "2012/01/17" and "2012/03/21". So, there is a way to do this?
I try:
string dateString = string.format("{0:d", "20120321");
and
string dateString = string.format("{0:yyyy/MM/dd", "20120321");
and
string dateString = int.Parse("20120321").ToString("yyyy/MM/dd");
I all cases i don't reach my goal. =/
So, i can i do this?
OBS: There is a way to do that without parse to datetime?
You have to parse those values in DateTime objects first.
Example :
DateTime dt = DateTime.ParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
var result = dt.ToString("yyyy/MM/dd");
Edit after your comments on other answers:
if you don't like parsing because it may throw excepations, you can always use TryParse, like this:
DateTime dt;
bool success = DateTime.TryParseExact("20120321", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
if (success)
{
var result = dt.ToString("yyyy/MM/dd");
}
Edit 2: Using TryParseExact with multiple formats:
DateTime dt;
string[] formats = { "yyyyMMdd", "yyyy" };
bool success = DateTime.TryParseExact("20120321", formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
if (success)
{
var result = dt.ToString("yyyy/MM/dd");
Console.WriteLine(result);
}
It will produce 2012/03/21 when using "20120321" as input value, and 2012/01/01 when using 2012 as input value.
DateTime.ParseExact("20120321", "yyyyMMdd", CultureInfo.CurrentCulture).ToString("yyyy/MM/dd")
You could parse the string but this method gives you validation without any extra code. Imagine receiving "20120230", "20110229", or any other invalid date.
From your comments:
There is a way to do that without parse to datetime?
Yes, absolutely. If you really want to propagate bad data through your system rather than highlighting that it's incorrect, you could definitely use:
// Make sure we'll always be able to get a result whatever the input.
string paddedInput = input + "????????";
string mightBeBadWhoKnows = string.Format("{0}/{1}/{2}",
paddedInput.Substring(0, 4), // The year, if we're lucky
paddedInput.Substring(4, 2), // The month, if we're lucky
paddedInput.Substring(6, 2)); // The day, if we're lucky
But why wouldn't you want to spot the bad data?
You absolutely should parse the data. If you want to be able to continue after receiving bad data having taken appropriate action, use DateTime.TryParseExact. If you're happy for an exception to be thrown, use DateTime.ParseExact. I'd suggest using the invariant culture for both parsing and formatting, unless you really want a culture-sensitive output.
Use DateTime.ParseExact to convert to a DateTime, then use ToString on that instance to format as you desire.
DateTime.ParseExact(dateString, "yyyyMMdd").ToString("yyyy/MM/dd");
EDIT: using Insert:
EDIT2: Fixed bugs :-)
var newString = dateString.Insert(4, "/").Insert(7, "/");
Just use string operations to insert the slashes:
string input = "20120321";
string dateString =
input.Substring(0, 4) + "/" +
input.Substring(4, 2) + "/" +
input.Substring(6);
or
string dateString = input.Insert(6, "/").Insert(4, "/");
If it's a date, try this:
DateTime.ParseExact("20120321","yyyyMMdd", null).ToString("yyyy/MM/dd", System.Globalization.DateTimeFormatInfo.InvariantInfo)
try this;
string dateString = DateTime.ParseExact("20120321", "yyyyMMdd",
null).ToShortDateString();
If your data is always in the same format and if you don't need to validate it, you can use the following snippet to avoid parsing it with DateTime
var strWithInsert = input.Insert(4,"/").Insert(7,"/");