Designate time by a 24-hour clock (eg, use 13:00 rather than 1:00 p.m.) with a colon
separating the minutes from hours (hh:mm).
Please find the below attachment.
Expected_output11.docx (11.9 KB)
Input_word_Document (1).docx (12.0 KB)
@Yuvarajshivananjappa You can use regular expression like the following to match AM/PM time format in your document
([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])\s*([AaPp][Mm])
And then you can use IReplacingCallback to get the matched value and convert it to the required time format.
Your given code is not working for my condition. Kindly help me
I have a word document in that I need to find the 12h format(eg:1:00pm) and convert into the 24h format (eg:13:00pm)(hh:mm).
Document doc = new Document("C:\\Users\\Yuvaraj\\Downloads\\Input_word_Document (1).docx");
FindReplaceOptions options = new FindReplaceOptions();
options.UseSubstitutions = true;
doc.Range.Replace(new Regex("([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])\\s*([AaPp][Mm])"), "$1:$2$3", options);
string dataDir = @"C:\Users\Yuvaraj\Downloads\Test_5.docx";
doc.Save(dataDir);
Any updates on this?
@Yuvarajshivananjappa Actually, this is not exactly what I have suggested. In your case you should use IReplacingCallback to reformat the matched time. For example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.ReplacingCallback = new DateTiemeReplacingCallback();
doc.Range.Replace(new Regex("([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])\\s*([AaPp][Mm])"), "", opt);
doc.Save(@"C:\Temp\out.docx");
private class DateTiemeReplacingCallback : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
string matchedString = args.Match.Value;
// Parse matched time into DateTime object.
DateTime matchedDate = DateTime.Parse(matchedString);
// Format time in 24 hours format.
args.Replacement = matchedDate.ToString("HH:mm");
return ReplaceAction.Replace;
}
}
Also, please note that you output time in 24-hours format there is no need in AM/PM at the end, like you specified in your expected output.
Your given code is just finding the 1:00 p.m. format and your converting 01:00
Above code is not converting 12h format to 24h format ,for example
Input : 1:00 p.m. or 2:00 p.m
Expected Output: 13:00 . or 14:00
@Princeshivananjappa, please find the updated code:
Document doc = new Document(@"in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.ReplacingCallback = new DateTiemeReplacingCallback();
doc.Range.Replace(new Regex("([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])\\s*([AaPp]\\.?[Mm]\\.?)"), "", opt);
doc.Save(@"out.docx");
private class DateTiemeReplacingCallback : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
string matchedString = args.Match.Value;
matchedString = matchedString.Replace(".", "");
// Parse matched time into DateTime object.
DateTime matchedDate = DateTime.Parse(matchedString);
// Format time in 24 hours format.
args.Replacement = matchedDate.ToString("HH:mm");
return ReplaceAction.Replace;
}
}
Thank you so much for your quick response.
@dshvydkiy Your given above code is overriding the correct data, no need to override the correct data Kindly help me asap.
Kindly find the below input and output word document.
out (2).docx (12.3 KB)
in (1).docx (12.5 KB)
Kindly check the below screenshot
@Princeshivananjappa You can add a condition into IReplacingCallback
implementation to filter matches that should not be replaced.
if (/*Your condition here*/)
return ReplaceAction.Skip;
Also, please note that if you output time in 24-hours format there is no need in AM/PM at the end.