How to get current utc time in kmm ? I tried below code but it gives time 01/01/1970 and it return 0 value.
DateTime.now().utc.milliseconds
If you're using kotlinx-datetime then simply doing Clock.System.now() would provide time in UTC.
Not sure what DateTime object you're using in your question!
Related
I've encountered a delay in the date that today() returns. I think it has something to do with the timezone but I don't know how to correct it.
For example, I use it today. I live in PH so it must return Oct 21. But it returns Oct 20. How do you I troubleshoot this?
File -> Settings -> Time Zone
Change to the location you want.
I am creating a time object, which I use to gather all info from the past 24 hours, I have no issue creating the object displaying the current time. But I am unsure as to how to set it exactly 24 hours in the past, without having the timezone attached.
def set_time
#past_time = Time.now.to_s(:db) - 1.days
end
Expected Output Format :
"2021-11-29 09:15:17"
Result:
undefined method `-' for "2021-11-29 10:19:46":String
You are subtracting the time from the string object as you converted Time.now into the string using to_s.
Instead of you can do this
(Time.new - 1.days).to_s(:db)
Note: You will get multiple ways to accomplish these rails. You can improve the code readability and understanding of code by doing this.
Example:
DateTime.now.days_ago(1)
The easiest I can think of would be:
24.hours.ago.to_s(:db)
Note that the returned time would default to UTC in this case.
You can use DateTime#advance from ActiveSupport:
Time.current.advance(hours: -24)
# or
Time.current.advance(days: -1)
Note that in timezones that use DST a day is not always 24 hours so the two are not actually equivilent. You can also use the methods that ActiveSupport::Duration monkeypatches onto Integer:
24.hours.ago
1.day.ago
This always uses your default timezone though.
I am working with Breeze and running into some date/time issues.
I have a field in a form, with a date time picker, that is returning a value of 07/20/2018 14:00. For this example, assume I am in CST timezone (GMT -0500). What I would like to do is pass that value to my Breeze entity manager and have it saved in my database correctly. I get the date into a variable:
dateVariable = ctx.ChosenTime;
This works and puts a value of 07/20/2018 14:00 into the variable dateVariable.
I create a new entity:
var newEntity = entityManager.createEntity('Test Entity', {Date: dateVariable};
And when I debug and check the value of newEntity, it has a property called Date with the proper value. However, once I call entityManager.SaveChanges(), and then get the returned value back, it is displayed as 07/20/2018 19:00. Since Breeze is handling the display value (via data binding), I am not sure why this is happening. Any advice would be appreciated. Thanks
When the JSON response comes from the server, if the date string does not have a timezone specifier, Breeze assumes it is in UTC and puts a "Z" on the end before parsing. So it converts your local time to UTC after the round-trip to the server. The solutions are:
Change your server-side property to DateTimeOffset or similar data type that preserves the timezone of a date. This way the returned date will have a time zone.
Tell Breeze not to add the "Z" and just interpret the date in local time. See this SO answer for more information.
I am getting some results from a weather API and one of that is date in epoch time stamp.
I found that converting with Date(timeIntervalSince1970:) I get the right date
I am using the specific number --> 1501452000 and I get 2 results on Playground
1) Jul 31,2017,12:00AM. -- when --> let date = Date(timeIntervalSince1970: 1501452000)
2) 2017-07-30 22:00:00 +0000 when --> print(date)
API results are :
"time_epoch": 1501452000,
"time": "2017-07-30 23:00",
By checking the rest of my results they are matching with the rest of the API results....... but when I convert 1501452000 -> to date I don't get the correct Hour 23:00 but 22:00 !
Any idea what is happening ?
is it wrong the API( I don't think so ) or the way I am converting it?
Thanks a lot
The timeIntervalSince1970 initializer sets up the time in the UTC timezone, while your API might be sending dates in GMT. When you are using print(data), you have different results, because if you are not using a DateFormatter to generate the String format of the Date object, it uses your devices current settings when formatting the Date object.
A Date object represents an absolute point in time, but when you are printing it with a DateFormatter, it gets converted into a location/time zone specific, relative representation. You just have to set up your DateFormatter to match the time zone settings of your API and you will see the dates correctly printed.
This issue happens on daylight saving times. Is your country changing daylight saving on this exact date?
let date = Date(timeIntervalSince1970: 1501452000) in Playgrounds should give you the time in your system's timezone when you see it on the right hand side.
When you print it and see 2017-07-30 22:00:00 +0000- this is the same timestamp in GMT
Is the API showing a particular time zone? It looks like GMT+1
I have the following record on my data base:
"availability_date" : ISODate("2014-09-29T15:45:00.000Z")
and I trying to get the differences between two datetime like this:
#minutes = (((#date_time.to_time) - (Time.now))/60).round
but the #date_time have the following value and I don't understand why???
"2014-09-29 17:45:00 +0200"
could someone help me please.
Thanks in advance
I don't think there is any problem, The datetime in #date_time is the same as in your database. The +0200 at the end means that it is written in a different timezone, here GMT +2, I guess. It is probably the time zone that your computer uses.
What is the result you expect ? Can you give an example ? And be sure to read the answer to In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?
Good luck.
Try to convert them to integer using to_i m this give you the number of seconds in unix time, the n it's probably more easy to do a calculation