Hi all,

How can I allow a user to modify his added comment within 30 minutes.

I mean by that, I'm allowing a user to pot a comment. This user may want to modify his comment. I, as administrator, need to allow this user to modify his comment if and only if his comment age is less than or equal 30 minutes.

Which method should I use?

Thanks in advance.

Dani AI

Generated

For : the reliable approach is server-side enforcement with a server-stamped creation time and an ownership check. As hinted, client-side timers are fine for UX, but they are not authoritative. Persist a CreatedAt timestamp in UTC (or use DateTimeOffset), record the comment owner, and always re-check on the server when the user submits the edit.

A minimal server-side example (C#) that illustrates the check:

if (comment.OwnerId != currentUserId) return Forbid();

var age = DateTimeOffset.UtcNow - comment.CreatedAtUtc;
if (age > TimeSpan.FromMinutes(30)) return BadRequest("Edit window expired.");

comment.Text = newText;
comment.UpdatedAtUtc = DateTimeOffset.UtcNow;
db.SaveChanges();

Design notes and edge cases:

  • Use a fixed window from creation (30 minutes after CreatedAt) unless your rules say otherwise. If you want the timer to start when the user clicks "Edit", store an EditStartedAt and check against that instead.
  • Never trust client-side hidden fields or the browser clock; they can be tampered with. Client JS may disable the edit UI after 30 minutes for a better UX, but the server must perform the final check.
  • Consider an admin override path (check role before denying) and log denied attempts for audit.
  • Store timestamps in UTC or as DateTimeOffset to avoid timezone confusion and daylight-saving issues.

For guidance on choosing timestamp types and cross-timezone best practices, see Microsoft's recommendations on DateTime vs DateTimeOffset:
.

I am assuming this is ASP.net question. I am further assuming that user enters comment and submits page. At this point, alongwith the comment, you would save time of entry.

Now there is another action (like click of button) user will perform in order to edit the comment. On click of that, on server side, you can check if it is already 30 min or less.

Same way, again on save of edited comment, you might want to check if change was submitted within 30 min from original request. You might not want to check (it depends on your requirements). For ex. if user asks edit on 28th min and doesn't submit till 32nd min. You might want to reset time stamp as well (again upto your requirements).

One can always use javascript, sending time of creation as hidden variable and start timer on load of page that will disable edit button after 30 min. The problem could be in time-zones. Like server is in California where your record added is in West-coast time. User is in New york. Half hr is already gone from your browser-to-server setting.

If this meets your requirements, glad I helped. Otherwise post what is missing (like "as admin" you mean soft coding 30min, you can use web.config or database etc)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.