How to find any date format in a string [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 3 years ago.
Improve this question
I need to get all dates in any formats from an string.
for example:
string text = "aaaaa25/01/2019 21:22:56 dfgdfv cvcvbcvb 25/02/18 2:12 asdfd";
I need a methods that extract all the dates
25/01/2019 21:22:56,
25/02/18 2:12
as DateTime.
I try by Regex:
var splitter = new Regex("[0-9]{2}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}");
foreach (Match m in splitter.Matches(str))
{
string dateString = m.Groups[0].Value;
DateTime dt = DateTime.ParseExact(dateString, "MM/dd/yy HH:mm:ss", null);
}
But I need to write any formats and there is a lot options.
I looking for method that detect any format of datetime.

/\d{1,2}\/\d{1,2}\/(?:\d{2}){1,2} \d{1,2}:\d{2}(:?:\d{2})?/gm
For a few pieces:
\d{1,2} matches 1 or two digits.
(?:\d{2}){1,2} matches a group of two digits once or twice. (therefore, 2 or 4 digits)
(?:\d{2}:)? allows the seconds to be optional

Related

How to get the date from the given string in 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 2 years ago.
Improve this question
I only need date from this string Wed, 02/05/2020 - 12:31 what format should I use whenever I am using
{MM/dd/yyyy} I am getting the same value if there is any other way please let me know
item.changed=Wed, 02/05/2020 - 12:31
`if (ListRecord.checkresponse == "Response Letters")
{
string checkresponse = item.changed;
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
}`
To use date formatting, you need to start with a DateTime object, not a string.
So in your scenario you'll have to parse your string as a DateTime, then format it out again to a different string format. There's no way to directly format string -> string, all the logic about formatting is in the DateTime class.
Here's an example:
string changed = "Wed, 02/05/2020 - 12:31";
var checkresponse = DateTime.ParseExact(changed, "ddd, MM/dd/yyyy - HH:mm", CultureInfo.InvariantCulture);
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
Console.WriteLine(issueDate);
Demo: https://dotnetfiddle.net/bhNImo
(Alternatively, since what you mainly want to do here is strip the first and last parts of the string and keep the date part, you could use a regular expression do to this from the string, but overall it's probably more reliably to use date parsing and formatting.)

I have a string like this "15.May-2019" and I want to replace May with 05 [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 2 years ago.
Improve this question
(so it looks like this "15.05-2019")but I don't want to remove the dot or the dash.How do I do this in C#
You could parse it to a DateTime and then format it back.
var result = DateTime.ParseExact("15.May-2019", "dd.MMMM-yyyy", System.Globalization.CultureInfo.InvariantCulture)
.ToString("dd.MM-yyyy");
This is useful if you need to handle dates with any month in them.
Note that the MMMM will work for long month names, but if you're dealing with short names MMM would be the way to go (for May the short and long names are the same). You can also use the overload of ParseExact that takes multiple formats if you need to handle both.
var str = "15.May-2019";
str = str.Replace("May", "05")
To replace part of a string you can do:
var date = "15.05-2019";
date = date.Replace("05", "May");
Console.WriteLine(date);
output : "15.May-2019"

Extract post numeric value from a string [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 5 years ago.
Improve this question
i want to get post digits from "KB"(Actual data size)
for example
string word1="Product data 5KB per second"
string word2="Product data is 5 KB per hour"
i want to extract 5 from the above words if i pass KB
This Regex should work
var word2Size = Regex.Match(word2, #"\d+(?=\s?KB)").Value;
or for floating point numbers
var word2Size = Regex.Match(word2, #"\d+\.?\d+(?=\s?KB)").Value;
it should return a numeric value immediately preceding optional whitespace followed by KB
You could generate a regex string for different suffix with :
string regexString = String.Format(#"\d+\.?\d+(?=\s?{0})", mySuffix);
var word2Size = Regex.Match(word2, regexString).Value;

Persian Calendar Regex Date Format [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 5 years ago.
Improve this question
I found a regular expression for the Persian Calendar in the link but it is in the yyyy/MM/dd format. Any suggestion to format to dd/MM/yyyy and MM/dd/yyyy?
(?:1[234]\d{2})(\/|\-)(?:0?[1-9]|1[0-2])(\/|\-)(?:0?[1-9]|[12][0-9]|3[01])$
Debuggex Demo
support year between 1200 and 1499
I am using this code and it works well:
First you define a constant like the following code:
public const string PersianDateRegex = #"(?:1[23]\d{2})\/(?:0?[1-9]|1[0-2])\/(?:0?[1-9]|[12][0-9]|3[01])$";
so for test it you can use the below code:
if (Regex.IsMatch(farsiDate, PersianDateRegex))
{
...
}
Try this in ASP.NET MVC for validation of a Persian date:
[RegularExpression(#"^$|^([1۱][۰-۹ 0-9]{3}[/\/]([0 ۰][۱-۶ 1-6])[/\/]([0 ۰][۱-۹ 1-9]|[۱۲12][۰-۹ 0-9]|[3۳][01۰۱])|[1۱][۰-۹ 0-9]{3}[/\/]([۰0][۷-۹ 7-9]|[1۱][۰۱۲012])[/\/]([۰0][1-9 ۱-۹]|[12۱۲][0-9 ۰-۹]|(30|۳۰)))$", ErrorMessage = "Not Valid")]

How do I format a string? [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
I have a string "5-13-2013"
I want to make it look like "051313"
All three fields (month,day,year) must have 2 digits, if it only has one , I need to add a zero in front
any easy way to do this?
You can convert your datetime to multiple formats like
string date = yourdateTime.ToString("dd/mm/yyyy");
string date = yourdateTime.ToString("dd/mm/yyyy HH:mm");
string date = yourdateTime.ToString("dd/mm/yyyy HH:mm:ss");
string date = yourdateTime.ToString("mmddyy"); // your desired
To know what these mean you need to read here.

Categories