2

I'm working on a single page chat application, and when displaying the message sent by some user ,I want all the other users to see the same exact time the message was sent in and also the correct time regardless user's computer time or any other reason. As I understood UNIX time do the job ,and I want to use it in my app and put it inside datetime HTML attribute . Is there a way to do that in HTML or should I use JS for that?

Note: I think that this question isn't relevant to me because first I want to use the attribute datetime and second thing I want to use Unix time .

Any help is appreciated.Thank you.

user8244016
  • 929
  • 3
  • 12
  • 24
  • Your referenced question is indeed relevant to you because *it is* talking about a unix timestamp. But what do you mean by datetime attribute? You want to use a ` – lumio Aug 20 '17 at 20:52
  • hi , I'm new to JS so I maybe was wrong about the question .. and yes I must use time tag and inside it use the attribute datetime – user8244016 Aug 20 '17 at 20:54
  • What did you try so far? Is the chat app fully client-based or are messages processed via a server? – yezzz Aug 20 '17 at 21:16
  • I don't know how to use the unix time , I read some materials but still don't know how to merge it with html.. my app is mostly client-based that means most functionalities are done in the client side , one of these functionalities are posting messages ..server is used very little – user6561572 Aug 21 '17 at 04:04

1 Answers1

2

Something like this?? You can set the dateTime attribute of the time tag with the date.toISOString function since it is one of the supported formats by the time tag.

var unixTime = 0; // fill in your epoc time here.
var isoTime = new Date(unixTime).toISOString()
timeEl.textContent = unixTime+' --- '+isoTime+' --- '+new Date(isoTime).toString();
timeEl.setAttribute('datetime', isoTime )
<time id="timeEl"></time>
joopmicroop
  • 891
  • 5
  • 15
  • hi thank you for replying,but what you wrote doesn't give me the current time?! – user8244016 Aug 21 '17 at 09:31
  • 1
    no it takes epoc 0, tought you wanted to fill in your own epoc times... var unixTime = new Date().getTime(); would give you the epoc time of the current date/time – joopmicroop Aug 21 '17 at 12:48