As the title reads, how can I convert a UNIX timestamp, e.g. 1458297862, into a Date object in Elm?
Date.fromString does not seem to accept it, and Date.fromTime gives the wrong answer.
You can use Date.fromTime, but you have to multiply that value by 1000.
This gives you the date you'd expect:
Date.fromTime 1458297862000
I came across this answer and it didn't quite work in 0.18.
I found that Date.fromTime wouldn't accept arbitrary integers. The reason is that it's expecting floats, so you'd really want:
Date.fromTime (toFloat 1458297862000)
Related
I know I can parse from Long from String like the following
"60".toLong
or convert Long from Double like the following
60.0.toLong
or convert Long from a String of Double like the following
"60.0".toDouble.toLong
However, I can't do the following
"60.0".toLong
So my question is whether using .toDouble.toLong is a best practice, or should I use something like try ... catch ...?
Meanwhile, there is another question, when I try to convert a very large Long to Double, there maybe some precision loss, I want to know how to fix that?
"9223372036854775800.31415926535827932".toDouble.toLong
You should wrap the operation in a Try anyway, in case the string is not valid.
What you do inside the Try depends on whether "60.0" is a valid value in your application.
If it is valid, use the two-step conversion.
Try("60.0".toDouble.toLong) // => Success(60)
If it is not valid, use the one-step version.
Try("60.0".toLong) // => Failure(java.lang.NumberFormatException...)
Answer to updated question:
9223372036854775800.31415926535827932 is outside the range for a Double, so you need BigDecimal for that.
Try(BigDecimal("9223372036854775800.31415926535827932").toLong)
However you are very close to maximum value for Long, so if the numbers really are that large I suggest avoiding Long and using BigDecimal and BigInt.
Try(BigDecimal("9223372036854775800.31415926535827932").toBigInt)
Note that toLong will not fail if the BigDecimal is too large, it just gives the wrong value.
This question already has answers here:
How can I format a String, which is a date, to a new date format in Java? [duplicate]
(1 answer)
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 4 years ago.
I want to convert the date from dd-mm-yy to yy-mm-dd in scala, but I am having issues to make it happen
val format = new java.text.SimpleDateFormat("dd-mm-yyyy")
format.format(new java.util.Date())
output:
res0: String = 06-07-2018
but if I want to change the format to yy-mm-dd:
val format = new java.text.SimpleDateFormat("yy-mm-dd")
format.format(new java.util.Date())
output:
res2: String = 18-41-06
what did I do wrong? why the mm comes out as 41?
The correct format is
val format = new java.text.SimpleDateFormat("yyyy-MM-dd")
what did I do wrong?
IMHO three things:
You used the long outdated Date class and the equally outdated and notoriously troublesome SimpleDateFormat. Instead use java.time, the modern Java date and time API.
You tried to roll your own date format. For most purposes your users will be happier with Java’s built-in date formats, and they save you the error-prone writing of your own format pattern string.
As others have already said, you used lowercase mm in the format pattern string. This is for minute of hour (format pattern letters are case sensitive). Apparently by coincidence you ran your first snippet at 7 minutes past the hour so it happened to agree with the current month (July).
Allow me first to demonstrate that java.time is a bit more helpful if you insist on writing your own format pattern and happen to use the wrong case (which happens for most of us from time to time). Sorry I cannot write Scala, you will have to do with Java.
DateTimeFormatter dateFormatterWithWrongCase
= DateTimeFormatter.ofPattern("dd-mm-yyyy");
LocalDate.now(ZoneId.of("America/Chicago")).format(dateFormatterWithWrongCase);
This throws the following:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfHour
It tells us we were trying to do something with the minute of hour, which we certainly didn’t intend. While it may not make the cause of the error very obvious at a glance, I prefer this by far over just getting an incorrect result. And the message is rather precise when you think about it since a LocalDate is a date without time of day and therefore doesn’t have minute of hour.
For getting a nice short date format for your users I suggest:
DateTimeFormatter dateFormatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(Locale.CANADA_FRENCH);
System.out.println(LocalDate.now(ZoneId.of("America/Chicago")).format(dateFormatter));
Today this printed:
18-07-07
One advantage (and I do mean an advantage) is that the output may be different in a different locale. If I use Locale.US, I get (M/d/yy):
7/7/18
To use the default locale of your JVM either specify Locale.getDefault() or just leave out the withLocale call on the formatter.
This may be the wrong place to ask this but I am aware of NSExpression, but it seems it can only do primitive mathematics - am I wrong about this? I am able to do something like this: #Value-2/3+9-6 ect easily, but I would like to incorporate Max's, Min's, and possible a few other operations (instead of just multiplication, division, subtraction, and addition. Is that possible in the same equation, does it have to be converted multiple times? Any advice would be appreciated!
You could try to use a "function" expression but that requires a rather unwieldy syntax in the expression string and is probably a stretch of the intended purpose of NSExpression unless you are actually implementing an aggregate type MIN/MAX function for predicates on a dataset.
I figured it out.
For anyone who needs this - you can take any string and use:
functionName(x) for most of the functions like sqrt, multiplyby, trunc, ceiling, ect
And then for the six with multiple variables (max, min, count, average, sum, ect) you use functionName({x,y}).
All can be used in a string with expressionValue(with: nil, context: nil)
how can i subtract number of days (timestamp|date'z') from current day (date('z')) in latte? I've tryes to use var but that does not like the formating (z).
Latte filters, unlike function calls, are not something that can be applied to any part of an expression – they are only optional feature of the variable printing macro.
{expression_to_be_printed|filter1|filter2|filter3}
date filter mostly just calls format method so you can use it directly:
{(new DateTime())->format('z') - $timestamp->format('z')}
This, however, will not work if the $timestamp lies in a different year.
To fix this, you can use DateTime’s diff method. DateInterval, returned by the method, can then be formatted using format method which provides difference in the number of days through %a formatting string.
{$timestamp->diff(new DateTime())->format('%a')}
Fortunately, the date filter also allows formatting intervals.
{$timestamp->diff(new DateTime())|date:'%a'}
Admittedly, this looks kind of ugly. A better way would be to define a custom filter so you could just use {$post->timestamp|daysAgo}. See Latte docs about creating your own filters.
Facebook's code changes on Tuesday night have impacted how parseInt works in FBJS. Where I previously used it to convert decimal numbers to straight integers, now it always returns undefined.
For example:
return parseInt(decimalnum);
no longer works. Anyone figured out how we are supposed to round to integers now? Thanks.
Thanks for the report. It's fixed on trunk now; it should be out tomorrow unless there's another push later today.
I suspect that decimalnum is not defined in your function. Try replacing your return with return decimalnum; -- you may still be returning undefined.
parseInt is not for rounding - it actually takes the integer component of a number, or coerces a string to be a number. If you want to round, use Math.round. Depending on your usage, you may find Math.floor or Math.ceil useful.
Math.floor()
Math.ceil()
Math.round()
parseInt()
Did you try parseInt(decimalnum, 10); ?