How can I convert a Date and a Time to a DateTime?
Say I have the following date and time
iex> date = ~D[2018-01-01]
iex> time = ~T[00:00:01.000]
How can I combine these to output the datetime: #DateTime<2018-01-01 00:00:01Z> in a clean way?
The best I come up with is using the Timex library:
Timex.add(Timex.to_datetime(date), Timex.Duration.from_time(time))
but I feel that surely there is a nicer, more readable way to combine this.
If you are using the calendar library, you can use either the Calendar.DateTime.from_date_and_time_and_zone function or the Calendar.NaiveDateTime.from_date_and_time function:
iex(4)> Calendar.DateTime.from_date_and_time_and_zone(~D[2018-10-01], ~T[12:22:22], "Australia/Melbourne")
{:ok, #DateTime<2018-10-01 12:22:22+10:00 AEST Australia/Melbourne>}
iex(5)> Calendar.NaiveDateTime.from_date_and_time(~D[2018-10-01], ~T[12:22:22])
{:ok, ~N[2018-10-01 12:22:22]}
There are also from_date_and_time! and from_date_and_time! variants.
You can use NaiveDateTime.new/2:
iex> NaiveDateTime.new(date, time)
{:ok, ~N[2018-01-01 00:00:01.000]}
The reason it is a "naive datetime" instead of a datetime is that the Time struct doesn't contain time zone information. If you know the time zone, you can add that information using DateTime.from_naive/2:
iex> DateTime.from_naive(~N[2018-01-01 00:00:01.000], "Etc/UTC")
{:ok, #DateTime<2018-01-01 00:00:01.000Z>}
While the answer by #legoscia is perfectly valid, here is how you deal with date and time pair (without Timex, just pure Elixir standard library):
date = ~D[2018-01-01]
time = ~T[00:00:01.000]
{Date.to_erl(date), Time.to_erl(time)}
|> NaiveDateTime.from_erl!()
|> DateTime.from_naive("Etc/UTC")
#⇒ {:ok, #DateTime<2018-01-01 00:00:01Z>}
Related
Let’s say I have an object of type Timestamp set to 2 days from now.
How to get the difference in time and then format it in DateFormat('hh : mm : ss') in dart ?
Construct a DateTime from the Timestamp.
Subtract from that DateTime the initial time (which I presume should be DateTime.now()) to get a Duration.
Format the Duration. Its default .toString() implementation uses hh:mm:ss.microseconds, which is close to what you want. If you want more control, you can easily format it yourself.
var dateTime = DateTime.fromMicrosecondsSinceEpoch(timestamp.timestamp!);
var duration = dateTime.difference(DateTime.now());
print(duration);
I'm using Scala along with play framework. I want to parse the String with simple date and say it's in UTC. So if I have 2018-10-04 I want to get 2018-10-04T00:00:00.000Z
With this:
DateTime.parse("2018-10-04", DateTimeFormat.forPattern("yyyy-mm-dd")).withZone(DateTimeZone.UTC)
I keep getting 2018-10-03T22:00:00.000Z if I have +2 timezone. How to just say that it's already in UTC?
One way is to use LocalDatetime to initially ignore the timezone:
scala> LocalDateTime.parse("2018-10-04", DateTimeFormat.forPattern("yyyy-MM-dd"))
res11: org.joda.time.LocalDateTime = 2018-10-04T00:00:00.000
then you can use toDateTime to make this UTC:
scala> LocalDateTime.parse("2018-10-04", DateTimeFormat.forPattern("yyyy-MM-dd")).toDateTime(DateTimeZone.UTC)
res12: org.joda.time.DateTime = 2018-10-04T00:00:00.000Z
Also: You should use MM (month) rather than mm (minute).
First the imports.
import org.joda.time.{DateTime, DateTimeZone}
import org.joda.time.format.DateTimeFormat
Since the time part is fixed, we can use a constant suffix for the time part.
val timeString = "T00:00:00.000Z"
We use string-interpolation to append this to the incoming dates.
DateTime.parse(s"2018-10-04$timeString", DateTimeFormat.forPattern("yyyy-mm-dd'T'HH:mm:ss.SSSZ")).withZone(DateTimeZone.UTC)
I'm trying to get records inserted after a certain date given to me by the client.
2018-06-06
Here's how I'm writing the query:
{:ok, date} = NaiveDateTime.from_iso8601(date_string)
from(
m in query,
where: m.inserted_at > ^date
)
(MatchError) no match of right hand side value: {:error, :invalid_format}
And when I try to use a simple Date object:
** (Ecto.Query.CastError) lib/messages/search.ex:77: value ~D[2018-06-06] in where cannot be cast to type :naive_datetime in query
How can I find all messages inserted after that dummy string date the client is passing me?
You have an ISO 8601 date there, not a datetime. You can convert it into a NaiveDateTime (with hour, minute, second all set to 0) like this:
iex(1)> date_string = "2018-06-06"
"2018-06-06"
iex(2)> ndt = NaiveDateTime.from_iso8601!(date_string <> " 00:00:00")
~N[2018-06-06 00:00:00]
Now you can use ndt in your query and it will work.
I don't understand why this date is saved as +1 day:
startdate = "2017-11-29T23:59:59.999Z";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives 30/11/2017
But if I do:
startdate = "2017-11-29";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives the correct date 29/11/2017
Any ideas?
Here is a jsfiddle showing this: http://jsfiddle.net/jbgUt/416/
Thanks!
If a time part is included, an offset from UTC can also be included as +-HH:mm, +-HHmm, +-HH or Z.
Add utc() to avoid it.
moment(startdate).utc().format('DD-MM-YYYY')
or
moment.utc(startdate).format('DD-MM-YYYY')
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()
Late to the party on this one, but I did just convert a few of our product's date-time objects to https://moment.github.io/luxon/
Takes out the need for the .utc() method above.
OK, I am trying to convert a date string from a format like:
2014-01-21 00:00:00
to
01/21/2014
I have tried many variations and am crashing and burning. The issue is that to test I have to create the script, export it in a process in Bonita (a BPM software), Import it and then create some cases. This all takes a long time.
Hopefully someone knows how to do this.
Also, is there a simple groovy editor out there? That would help me learn how to write groovy very quickly.
Groovy Dates have methods parse and format for converting to and from strings in various formats:
def format1 = '2014-01-21 00:00:00'
def format2 = Date.parse("yyyy-MM-dd hh:mm:ss", format1).format("dd/MM/yyyy")
assert format2 == '01/21/2014'
The format of the format strings are the same as Java's SimpleDateFormat.
String olddate='2014/01/21 00:00:00'
Date date = Date.parse("yyyy/MM/dd HH:mm:ss",olddate)
String newDate = date.format( 'MM/dd/yyyy' )
log.info newDate