How to extract comments datetime with seconds as well in .docx in c#

How to extract comments datetime with seconds as well in .docx in c#

@cpai.sachin You can use Comment.DateTime property to get date and time of the comment.

Thanks @alexey.noskov for your reply.
I am using the same but getting the datetime as “2023-05-23 13:23:00.000”
I want datetime as “2023-05-23 13:23:45.000”
Can you suggest the convenient way for the same.
thank you!

@cpai.sachin Could you please attach your input document here for testing?

Sure.
PFA .docx for further reference.TestDocumentForCommentDateTime.docx (7.5 KB)

@cpai.sachin I have used the following code for testing:

Document doc = new Document(@"C:\Temp\in.docx");

foreach (Comment c in doc.GetChildNodes(NodeType.Comment, true))
    Console.WriteLine(c.DateTime);

it returns the following result:

5/23/2023 14:16:23
5/23/2023 14:18:13

That corresponds values stored in the source document:

...
<w:comment w:author="Sachin Gangarde" w:id="0" w:date="2023-05-23T14:16:23Z">
...
<w:comment w:author="Sachin Gangarde" w:id="1" w:date="2023-05-23T14:18:13Z">
...

okay @alexey.noskov, let me check and thank you.

1 Like

Hi @alexey.noskov when we append the comment using Comment class we are getting output like this

can you help me to bind datetime in following format 2023-05-23T14:16:23Z
thank you.

@cpai.sachin The following code:

Document doc = new Document();

Comment comment = new Comment(doc, "Alexey", "AN", new DateTime(2023, 05, 23, 14, 16, 23));
comment.AppendChild(new Paragraph(doc));
comment.FirstParagraph.AppendChild(new Run(doc, "My comment"));

doc.FirstSection.Body.FirstParagraph.AppendChild(comment);

doc.Save(@"C:\Temp\out.docx");

produces comment like this in the output document:

<w:comment w:id="0" w:author="Alexey" w:date="2023-05-23T14:16:23Z" w:initials="AN">
	<w:p>
		<w:r>
			<w:t>My comment</w:t>
		</w:r>
	</w:p>
</w:comment>

Thanks @alexey.noskov.

1 Like

Hi @alexey.noskov I don’t know why above code is not giving me the expected datetime format

code sample–

I am doing this in the existing document I mean, appending the comment in the existing document.

For fresh document its working but for existing docx it not showing expected datetime format

@cpai.sachin Could you please attach your ample input and output documents here for testing? We will check the issue and provide you more information.
I have tried with the document you had attached earlier as an input and the added comment has correct date on my side.

Hi @alexey.noskov sure.
PFA docx for your further reference.TestDocuments.docx (14.1 KB)

could you please attach comments in the attached docx and check.

@cpai.sachin Thank you for additional information. I see in the attached document date of the comment does not have seconds - w:date="2023-05-23T14:16:00Z". But if I add comment using the code I have provided above, the comment date is correct - w:date="2023-05-23T14:16:23Z".
If possible, please create a simple application that will allow us to reproduce the problem and attach it here.

1 Like