I want to select all formats for date and time and i need a full regex.
currently i am using this regex:
public const string DateTimeRegex = #"\b(?<datetime>" +
// Date part
#"((" +
#"(\d{1,2}[\/\-\.]\d{1,2}[\/\-\.]\d{2}(\d{2})?)" +
#"|(\d{1,2}\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-zA-Z]*,?\s+\d{2}\d{2}?)" +
#"|((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-zA-Z]*\s+\d{1,2},?\s+\d{2}\d{2}?)" +
#"|(\d{4}\-\d{1,2}-\d{1,2})" +
#")" +
// Optional time part
#"(" +
#"[T\s]?\d{1,2}:\d{1,2}(:\d{1,2}(\.\d+)?)?(([AP]M)|([\+\-][12]?\d:\d{1,2})|Z)?" +
#")?)" +
#")\b";
I have a little problem with this regex, it correctly selects all the dates in the following format, but it cannot select the time (only a few are selected)
11/17/2022 11:36 AM
1/6/2023 6:19 PM
11/3/2022 12:06 PM
Saturday, December 10, 2022 8:07 AM
Thursday, January 5, 2023 3:27 AM
output:
11/17/2022
1/6/2023
11/3/2022
December 10, 2022
January 5, 2023
so i need to use another regex for selecting time part
public const string DateTimeRegex2 = #"(\d{1,2}:\d{1,2}(:\d{1,2}(\.\d+)?)?(([AP]M)|([\+\-][12]?\d:\d{1,2})|Z)?)";
I tried to merge these 2 regex but unfortunately it doesn't work and date and time are not selected correctly.
now i have a date like this:
25-Dec-2022 12:36
and None of the regexes can not select it.
so I want a regex that selects the following formats:
25-Dec-2022
25-Dec-2022 12:36
25-Dec-2022 12:36 PM
11/17/2022
11/17/2022 12:36
11/17/2022 12:36 PM
1/6/2023
1/6/2023 12:36
1/6/2023 12:36 PM
11/3/2022
11/3/2022 12:36
11/3/2022 12:36 PM
December 10, 2022
December 10, 2022 12:36
December 10, 2022 12:36 PM
January 5, 2023
January 5, 2023 12:36
January 5, 2023 12:36 PM
If any other formats are available, I need to identify them
What about the following RegEx?
(?<date>(?:\d{1,2}\/\d{1,2}\/\d{4}|\d{1,2}-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}|(?:January|February|March|April|May|June|Jule|August|September|October|November|December) \d{1,2}, \d{4}))(?:\s+(?<time>\d{1,2}:\d{1,2}(?: (?:AM|PM))?))?
Related
I have string in "Mon, 20 Mar 2021 14:04:48 +0000"
and I want to convert it as "20 Mar 2021 | 14:04 PM"
I want to convert the string as it is but it was appearing differently in my local and server.
First of all 20 Mar 2021 is Saturday, not Monday, let's correct it. Then you can ParseExact to get DateTime and finally represent it in the required format with a help of ToString():
string source = "Sat, 20 Mar 2021 14:04:48 +0000";
string result = DateTime
.ParseExact(source, "ddd, dd MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture)
.ToUniversalTime()
.ToString("dd MMM yyyy' | 'HH:mm tt", CultureInfo.InvariantCulture);
Notes:
It seems that you want to obtain Universal (not Local) time, that's why I've added ToUniversalTime()
14:04 PM looks strange for me (14:04 and 02:04 PM are much more frequent formats); put hh instead of HH to have 02:04 PM
If you actually want to be manipulating the timezone information, then use Noda Time.
If that's your exact text format, and the dates and times are what you want, you can convert it manually:
var input = "Mon, 20 Mar 2021 14:04:48 +0000";
var dateParts = input.Split(' ');
var timeParts = dateParts[4].Split(':');
var amPm = int.Parse(timeParts[0]) < 12 ? "AM" : "PM";
var output = $"{dateParts[1]} {dateParts[2]} {dateParts[3]} | {timeParts[0]}:{timeParts[1]} {amPm}";
System.Console.WriteLine(output); // "20 Mar 2021 | 14:04 PM"
Or, if you are feel adventurous, use a regular expression.
I'm trying to parse a date in my string using DateTime.TryParseExact. The string looks like this:
Wed, 21 Apr 2019 07:28:45 GMT
However, the parse function always fails when I use the ddd format specifier. If I remove 'Wed' from the string and the 'ddd' part from the format string, it parses just fine.
Here's a bit of code that reproduces the problem
var ci = CultureInfo.CreateSpecificCulture("en-GB");
var datesToParse = new[] { "Wed, 21 Apr 2019 07:28:45 GMT", "21 Apr 2019 07:28:45 GMT" };
var formats = new[] { "ddd, dd MMM yyyy HH:mm:ss 'GMT'", "dd MMM yyyy HH:mm:ss 'GMT'" };
foreach (var dateToParse in datesToParse)
{
var result = DateTime.TryParseExact(dateToParse, formats, ci, DateTimeStyles.AllowWhiteSpaces, out DateTime parsedDate)
? parsedDate.ToLongDateString()
: $"Unable to parse date: {dateToParse}";
Console.WriteLine(result);
}
Console.ReadLine();
And the output:
Unable to parse date: Wed, 21 Apr 2019 07:28:45 GMT
21 April 2019
I'm not really sure what I'm doing wrong here. Any help would be appreciated.
edit: improved the output and program syntax
Your issue is that 21 April 2019 was a Sunday and not a Wednesday. This works fine "Sun, 21 Apr 2019 07:28:45 GMT"
I'm trying to figure out the format of few dates that are available in numeric format. I need to convert them to exact dates. I have a sample dates that I was trying to convert:
1443506173.0 >> Sep 29, 2015
1443505895.0 >> Sep 29, 2015
1441805416.0 >> Sep 09, 2015
1438174556.0 >> Jul 29, 2015
1436476814.0 >> Jul 10, 2015
1414994162.0 >> Nov 03, 2014
1413294207.0 >> Oct 14, 2014
By looking at the first two entries, I can see that the numbers are changing but both are representing the same dates. Means there must be time embedded into this date format. Currently I'm concerned with extracting only date, I dont need to extract time at the moment. Would be great if it was simple enough to extract time as well.
Can anyone help figure this out? In case you're wondering, I got these dates from Instagram posts feed. Using WebClient I downloaded an Instagram photo's URL. This date format is there in the scripts section. If I'm able to decode this date format, I would know what the date (and time) of the photo post was at Instagram.
Thanks in advance. I'm using C# to perform this conversion.
Instagram uses the Unix Timestamp for its format (the number of seconds since 1st January 1970).
One method of converting this to a DateTime object would be:
DateTime dateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(yourUnixTimestampValue).ToLocalTime();
This is a UNIX Timestamp
void Main()
{
var timestamps = new[]{
new {stamp = 1443506173.0, datetime = new DateTime(2015, 9, 29)},
new {stamp = 1443505895.0, datetime = new DateTime(2015, 9, 29)},
new {stamp = 1413294207.0, datetime = new DateTime(2014, 10, 14)}
};
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
foreach (var i in timestamps)
{
Console.WriteLine("Item: {0}, Converted: {1}", i, new DateTime(1970, 1,1).AddSeconds(i.stamp).ToLongDateString());
}
}
Output:
Item: { stamp = 1443506173, datetime = 09/29/2015 00:00:00 }, Converted: Tuesday, 29 September 2015
Item: { stamp = 1443505895, datetime = 09/29/2015 00:00:00 }, Converted: Tuesday, 29 September 2015
Item: { stamp = 1413294207, datetime = 10/14/2014 00:00:00 }, Converted: Tuesday, 14 October 2014
I read a datetime from text file with this format
Thu Apr 16 09:55:44 2015
How can I format this like 16/04/2015 09:55:44 ?
Do it in two steps. Use DateTime.ParseExact to parse the original string, then use ToString() to output it in the other desired format:
var input = "Thu Apr 16 09:55:44 2015";
var parsed =
DateTime.ParseExact(input, "ddd MMM dd HH:mm:ss yyyy", CultureInfo.InvariantCulture);
var output = parsed.ToString("dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
First of all you need to convert to DateTime:
https://msdn.microsoft.com/en-us/library/cc165448.aspx
Then you need to convert to String again, using the correctly format, as you can see here:
https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx
// This example displays the following output to the console:
// d: 6/15/2008
// D: Sunday, June 15, 2008
// f: Sunday, June 15, 2008 9:15 PM
// F: Sunday, June 15, 2008 9:15:07 PM
// g: 6/15/2008 9:15 PM
// G: 6/15/2008 9:15:07 PM
// m: June 15
// o: 2008-06-15T21:15:07.0000000
// R: Sun, 15 Jun 2008 21:15:07 GMT
// s: 2008-06-15T21:15:07
// t: 9:15 PM
// T: 9:15:07 PM
// u: 2008-06-15 21:15:07Z
// U: Monday, June 16, 2008 4:15:07 AM
// y: June, 2008
//
// 'h:mm:ss.ff t': 9:15:07.00 P
// 'd MMM yyyy': 15 Jun 2008
// 'HH:mm:ss.f': 21:15:07.0
// 'dd MMM HH:mm:ss': 15 Jun 21:15:07
// '\Mon\t\h\: M': Month: 6
// 'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00
My C# application send a formatted date (ex: 9/6/2010 - dd/mm/yyyy) to an excel spreadsheet... but then... it comes to 6/9/2010! (mm/dd/yyyy)
I just use the following code to send the date:
VarRowColumnWhatever = _MyList.MyObject.MyDateTime.Date.ToString();
Debbuging it, i can see that the value are correct! But not in the spreadsheet =(
I began to think that is something to do with Excel...
Someone please can help me? Thanks!
Stick a string according to the pattern you want into the ToString() portion.
For instance, to get the Full Day, Month, Date, Year, i'd put:
_MyList.MyObject.MyDateTime.TimeOfDay.ToString("D");
which would output the string: "Wednesday, June 23, 2010"
// This example displays the following output to the console:
// d: 6/15/2008
// D: Sunday, June 15, 2008
// f: Sunday, June 15, 2008 9:15 PM
// F: Sunday, June 15, 2008 9:15:07 PM
// g: 6/15/2008 9:15 PM
// G: 6/15/2008 9:15:07 PM
// m: June 15
// o: 2008-06-15T21:15:07.0000000
// R: Sun, 15 Jun 2008 21:15:07 GMT
// s: 2008-06-15T21:15:07
// t: 9:15 PM
// T: 9:15:07 PM
// u: 2008-06-15 21:15:07Z
// U: Monday, June 16, 2008 4:15:07 AM
// y: June, 2008
//
// 'h:mm:ss.ff t': 9:15:07.00 P
// 'd MMM yyyy': 15 Jun 2008
// 'HH:mm:ss.f': 21:15:07.0
// 'dd MMM HH:mm:ss': 15 Jun 21:15:07
// '\Mon\t\h: M': Month: 6
// 'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00
Try to write your code like this:
VarRowColumnWhatever = _MyList.MyObject.MyDateTime.Date.ToString("dd\/MM\/yyyy");
or make sure your culture is set to one with your desired format.
If the Excel cell you are putting the value into is of Date type in Excel (if using interop), then you should use:
VarRowColumnWhatever = _MyList.MyObject.MyDateTime.Date.ToOADate();