Getting invalid date from moment.js? - momentjs

I have a redis stored timestamp when I get it, it looks like this:
1454803149444
Then when I try to do:
i.text-muted.createdAtPost= moment(post.timestamp).format("MM/DD/YY # h:mm:ss") // Jade Template
I get
Invalid Date
But if I take the same integer, and go moment(1454803149444).format(h:mm:ss") I get 05/21/54 # 12:00:00
Any information would be great thanks.

I had the same problem. What I did was
let time = post.timestamp / 1000;
let formatted = moment.unix(time).format("MM/DD/YY # h:mm:ss");
I have no idea why this works. I tried forcing the value into an integer and still got invalid date. I hard coded the value that was console logged and it worked, like in your case. For some reason dividing it by 1000 and calling it a unix timestamp works fine. Seems like a bug to me.

What value does timestamp pass?
Did you check that?
If it has a specific format you need to set it as:
moment(post.timestamp, 'YYYY/DD/MM').format("MM/DD/YY # h:mm:ss")

Related

How to convert Unix Time to Human Readable in Integromat?

My preceding module in Integromat gives me an expiration date in UNIX time, which is 1640930400.
When I use the FormatDate function to convert this, I'm getting 12/31/1969 when I was expecting to get 12/31/2021. I can't seem to figure out what I'm doing wrong here. Any help would be much appreciated.
Use this instead to first parse the date and then apply the desired formatting to get the results that you want,
{{formatDate(parseDate(1.date; "X"); "MM/DD/YYYY")}}

Format Date via Parameter

I have a DateTime variable (default formatting), and I would like to format it to a format to I receive from a string parameter.
I normally do something similar to: {myDate:yyyy-MM-dd}, and it works properly.
Now I have a lot of possible date formats and need to format according to the chosen one.
I have tried the following but returned garbage (ae0aor0aa):
string testFormat = "yyyy. MM. dd.";
{myDate:testFormat }
I have also tried to convert the date to string and back to date with ParseExact, but gave me an invalid date exception. NB: the date in myDate is valid, as I have checked it with the debugger.
Can you kindly advise?
Thanks to apc, it was easily solved by myDate.ToString(testFormat)

USQL reading last n days when file name pattern does not have day part

In data lake I have file names with pattern yyyyMM_data.csv. Now I want to read previous 3 days data. I am using below code -
DECLARE #ReportDate DateTime= DateTime.Parse("05/08/2017");
DECLARE #FeatureSummaryInput string=#"/FolderPath/{InputFileDate:yyyy}{InputFileDate:MM}_data.csv";
#FeaturedUsed =
EXTRACT Id string,InputFileDate DateTime
FROM #FeatureSummaryInput
USING Extractors.Csv(silent : true, skipFirstNRows : 1);
#FeaturedUsed=
SELECT *
FROM #FeaturedUsed
WHERE InputFileDate BETWEEN #ReportDate.AddDays(-3) AND #ReportDate;
If I run above code it runs with empty input. Please let me know if I am missing something. Why it is not reading correct file?
It seems like we need to must have "day" in file name pattern to work this.
Possibly I am missing something but, as you cast InputFileDate to DateTime it defaults to the first of the month, as no day is specified. For your test ReportDate set to 05/08/2017, your WHERE clause basically evaluates to Between 2017-08-02 And 2017-08-05, which will never be true.
Where do you expect the day element to come in with your files structured as yyyyMM?

moment.js and deprecated warning. timestamp to moment date object

I've read thru various posts on here in regards to similiar issues but none have solved my problem.
I manipulate the moment.js date object, and then store it as timestamp.
BUT, when I try to read in that timestamp again, I get that deprecated warning.
""Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info."
I've tried toDate(), format(), moment(myTimeStamp, 'ddd, DD MMM YYYY HH:mm:ss ZZ'); --> all generate the warning...
So, for example, my timestamp will look like this:
const timestamp = '1458586740000'
when I read that back and try to parse out the month/day/year, then the hour/min am/pm, etc... I need to get that timestamp into a moment.js object. Nothing is working for me. Any ideas.
How can I get this timestamp: '1458586740000', into a moment.js object so I can extract date date from it as I need?
EDIT: this is how I am storing the timestamp. So I would need to retrieve it from this.
let timeStamp = Moment(state[_Date])
.add({ hour: state[AMPM] === 'PM'
? +state[Hour] + 12
: state[Hour] ,
minute: state[Min] }).format('x')
The X token indicates a unix timestamp in seconds, and the x token indicates a unix millisecond timestamp (offset).
You appear to have a millisecond timestamp, so you would make a moment out of it by doing the following:
var a = moment('1458586740000', 'x')
It works without ' as well:
var a = moment(1458586740000, 'x')
You can also not specify the x and it should work:
moment(1458586740000)
Because you have a unix offset (milliseconds), not a unix timestamp (seconds), moment.unix is not what you want.
Then you can do the following:
a.format()
"2016-03-21T13:59:00-05:00"
Or you can use any of the other formatting tokens listed here to output whatever result you would like: http://momentjs.com/docs/#/displaying/format/
Based on the code you presented, I think you may be having problems because your timestamp is stored as a string (in ''). Parsing as a string causes an invalid date error, because it attempts to match ISO 8601 format and fails. Specifying that 'x' token will cause it to assume unix offset and work correctly.

Comparing dates in Lingo

How do I compare two dates in Lingo? To be specific, I want to know if today's date is after some fixed date. I know I can create the fixed date by using:
date("20090101")
and I can get the current date using:
_system.date()
but I can't seem to directly compare the two. Do I have to parse the _system.date() to determine if it's after my fixed date? I tried:
if(_system.date() > date("20090101") then
--do something
end if
but that doesn't seem to work. Any ideas?
Instead of _system.date(), try _movie.systemDate(), it will return a date object that you can safely compare with another one.
if _movie.systemDate() > date("20090101") then
--do something
end if
regards
I ended up doing the following. Inelegant, but it works:
if (_system.date().char[1..2] >= 01 and _system.date().char[4..5] >= 01 and _system.date().char[7..10] >= 2010) then
alert("Your license has expired. Please contact the Company to renew your license.")
_player.quit()
end if
It does the trick, but I would still be interested in alternative methods of doing this.

Resources