How to use DateTime

I have no idea how to use the DateTime and i have looked in the devhub but it makes no sense to me…
(basically can i get a tutorial on how to use it including code and explanations would be the best)

1 Like

DateTime uses local date and time to the client. What exactly do you want to know?

how to use it in code like for example i have no idea how to use it as tick()

Well, all the information you need is in the DevHub article you mentioned. You really just have to mess around with it to understand it. There is another article for DateTime as well that may be of use.

If you are wanting to use it like tick(), then you can use the DateTime.now() function. Just be warned that it uses milliseconds (I think), so you will have to account for that.

1 Like

i didnt know about the second article so thanks for showing me that one but i just dont get the first one and i have been trying for long kinda makes no sense to me

DateTime is an exact time partitioned between sections Year, Month, Day etc. To use it instead of tick() would be very tedious. Is that your intent? The only practical use for DateTime would be to simply display it to the client, making a game that uses the time of day of client to display day or night for them, using an actual countdown to an event like new years in their local time, etc. As @BuilderBob25620 outlined, there is no definite answer unless you have a goal to use it for. Just play around with it really.

i have heard that tick() will (or already is) deprecated and that datetime is the “new thing” so i was thinking of using it for timing stuff (also datetime has milliseconds so it can be tightly timed etc.)

I haven’t heard that before, but if you want a replacement then use

DateTime.now().UnixTimestamp -- The time since the UNIX Epoch in seconds
DateTime.now().UnixTimestampMillis -- The time since the UNIX Epoch in milliseconds
1 Like

tick() is a UNIX epoch, so no it’s not at all deprecated. It’s integrated into your computer already as is and it’s use in real world things such as your own clock on your system. I doubt that could possibly get deprecated.

1 Like

would i have to call DateTime.now() every time?

tick() has only seconds tho as far as i know but datetime has milliseconds (kinda pointless imo but could that be worth it?)

What do you mean? You mean every time you want to get the current time? If so, then yes. However, if you mean to do so for something like a timing system, then no. In that case you would do something like this:

local StartTime = DateTime.now().UnixTimestampMillis

-- Do stuff

local EndTime = DateTime.now().UnixTimestampMillis

print("Total Time: ",(EndTime-StartTime)/1000)
1 Like

also im just wondering why is there a fromUnixTimestampMillis() (and the other one) and how would i use that (my last question)

Those are constructors for creating the DateTime object, just like DateTime.now(). If you are unfamiliar with what constructors are then you should learn about Object-Oriented Programming. Also, if you read the documentation then you can see whether a variable is a method of DateTime, a property of the DateTime Object, or a constructor that creates the DateTime Object.

1 Like

The difference between tick() and DateTime applied:

local tick_ = tick()
local DTtime = DateTime.now().UnixTimestamp
-- run a bunch of stuff
print ("Time elapsed with tick() "..math.abs(tick_ - tick()))
print("Time elasped with DateTime "..DateTime.fromUnixTimestamp(DTtime))

same outputs in number but the formatting and the way it looks is different. The milliseconds is just the same thing but using milliseconds instead of seconds. More precise Ig

3 Likes

nvm i realised njesk showed the answer

Just a quick suggestion, the proper constructor for the code is DateTime.now() instead of DateTime.new()

1 Like

The constructor is basically a function that returns a DateTime object, much like how Vector3.new() returns a Vector3 value.

1 Like

i didnt see it at first but @njesk12 answered it so its fine i get it now thanks both for helping me

All in all the documentation had all that @BuilderBob25620 and I said so I recommend you reading the OOP article BuilderBob linked. It’s essential to understanding basically everything in scripting.

1 Like