I'm trying to get a date in milliseconds with moment.js. The problem is, in their docs in unix timestamp section they have only unix timestampt to momentjs format. How can I get a unix timestamp in milliseconds from date?
Eg: moment.unix(2010-01-01T05:06:07) => 1262318767000
You can get the date in milliseconds also with format().
moment("2010-01-01T05:06:07").format('x');
You said that "in their docs in unix timestamp section they have only unix timestampt to momentjs format", but that's not true.
According to https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/:
moment#valueOf simply outputs the number of milliseconds since the Unix Epoch
So, use the valueOf method:
moment("2010-01-01T05:06:07").valueOf();
But in my machine it returns 1262329567000. That's because moment.js is using my browser's timezone - this milliseconds value corresponds to January 1st 2010, at 05:06:07 AM in my browser's timezone.
The value you mention in your question (1262318767000) corresponds to January 1st 2010, at 05:06:07 AM in a timezone where the offset +01:00 is used: in some place that is one hour ahead of UTC, in January 1st 2010: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets#UTC+01:00,_A
If you want to be specific about what timezone the date/time corresponds to, you can use moment timezone: https://momentjs.com/timezone/
valueOf function https://momentjs.com/docs/#/parsing/utc/
console.log(moment('12/09/2021').valueOf());
In above example, you will not if it is 12 September or 09 January. It is better to provide the format, which format you're sending the value.
Checkout the below example,
moment(dateValue, dateFormat).valueOf();
So here we provide the date format which we are sending to moment and again converting it to a format moment understands and then taking out the milliseconds from 1 January 1970. If your date is less than that date, the value will be negative
Example:
console.log(moment('09/12/2021', 'DD/MM/YYYY').valueOf());
You can use
moment( date ).toDate().getTime();
And if you like to use timezone
let timezone = moment.tz.guess() || 'America/Los_Angeles';
moment( date, timezone ).toDate().getTime();
Related
I am seeing error the input "06/09/22 02:14 CDT" can't be parsed as format MM/dd/yy HH:mm ZZZZ` when trying to get luxon date time from string.
DateTime.fromFormat("06/09/22 02:14 CDT","MM/dd/yy HH:mm ZZZZ")
Not sure what is the valid format I need to use when there is time zone in date string.
Thanks.
Issue is that you input contains CDT that is not recognized by Luxon since ZZZZ is not a valid token as explained in the Parsing -> Limitations section of the docs:
Not every token supported by DateTime#toFormat is supported in the parser. For example, there's no ZZZZ or ZZZZZ tokens. This is for a few reasons:
Luxon relies on natively-available functionality that only provides the mapping in one direction. We can ask what the named offset is and get "Eastern Standard Time" but not ask what "Eastern Standard Time" is most likely to mean.
Some things are ambiguous. There are several Eastern Standard Times in different countries and Luxon has no way to know which one you mean without additional information (such as that the zone is America/New_York) that would make EST superfluous anyway. Similarly, the single-letter month and weekday formats (EEEEE) that are useful in displaying calendars graphically can't be parsed because of their ambiguity.
You can add fixed string 'CDT' in your format or remove it completely from your input. You can use zone option (America/New_York in the example above, or America/Chicago in your use case) of DateTime#toFormat to take into account timezone offset.
Example:
const DateTime = luxon.DateTime;
const dt1 = DateTime.fromFormat("06/09/22 02:14 CDT","MM/dd/yy HH:mm 'CDT'", {zone: 'America/Chicago'})
const dt2 = DateTime.fromFormat("06/09/22 02:14", "MM/dd/yy HH:mm", {zone: 'America/Chicago'})
console.log(dt1.toISO());
console.log(dt2.toISO());
<script src="https://cdn.jsdelivr.net/npm/luxon#2.4.0/build/global/luxon.min.js"></script>
Can't format the below date using moment.js, the below statement returns Invalid Date
moment('20171206T062406927Z').format('D-MMM-YYYY');
Please help me on this.
You need to tell moment which format your date string is in:
moment('20171206T062406927Z', 'YYYYMMDD[T]HHmmssSSSZ', true).format('D-MMM-YYYY');
Edit: updated as per #VincenzoC comment to ensure the timestamp is parsed in UTC
Also fix: use HH for 24-hour format (not hh), and pass a third true parameter to ensure the timestamp is parsed in strict mode.
I insert timestamps in a column of type real. The timestamps are in unixepoch format, eg 1505720496876 which is GMT: Monday, September 18, 2017 7:41:36.876 AM.
However, when I perform the query
select datetime(timestamp, 'unixepoch', 'localtime') from history I get -1413-03-01 14:07:12 as result.
What am I doing wrong?
You are currently using milliseconds but you need to use seconds:
select datetime(timestamp / 1000, 'unixepoch', 'localtime')
from history
From the documentation:
The "unixepoch" modifier (11) only works if it immediately follows a timestring in the DDDDDDDDDD format. This modifier causes the DDDDDDDDDD to be interpreted not as a Julian day number as it normally would be, but as Unix Time - the number of seconds since 1970.
I have a scenario where i need to convert a Datefield(joindate) to currentcompany timezone date. And then i need to compare this with anotherdate(startdate). If the difference is more than 365 days i need to give an warning. Can someone help me in this.
Thanks in advance.
You can apply a timezone to an utcdatetime via DateTimeUtil::applyTimeZoneOffset
The company timezone can be retrieved by calling DateTimeUtil::getCompanyTimeZone
Afterwards calculate the difference by calling DateTimeUtil::getDifference, which returns the difference in seconds so you have to compare that with the seconds per year.
To avoid inserting a 'magic' number, use the constants in the macro library TimeConstants.
If Datefield(joindate) is of type date and not utcDateTime then DateTimeUtil::newDateTime() should be used to convert it to utcDateTime:
utcDateTime joinDateTime = DateTimeUtil::newDateTime(joindate, 0,
DateTimeUtil::getCompanyTimeZone());
DateTimeUtil::getDifference() can be used to get the number of seconds between the utcDateTime values.
If both Datefield(joindate) and anotherdate(startdate) are of type date and not utcDateType then no conversion is required at all, and you can check whether the difference is more than 365 as follows:
if (joindate - startdate > 365) {}
If the above assumptions are wrong, see DAXaholic's answer.
I want to be able to store for example 2:30pm in a column in SQLite, and be able to pull that time and convert it to seconds, does SQLite have a function for this?
I was reading http://www.sqlite.org/datatype3.html and they do state they have a time function, but can I do this? I know that SQLite does not have a time datatype, but what type should I store it as then, varchar?
2:30pm --> 52200
I was reading more into this: http://www.sqlite.org/lang_datefunc.html
and it seems like the list of time acceptable is :
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
So does that mean you can't use 2:30pm as a format?
Since you will need the value to be in seconds from the beginning of the day (per your clarification) you can just store as an integer. To convert it to seconds from the beginning of the day rather than 1970 simply subtract midnight of the current day from it.
Something like this
strftime('%s','2004-01-01 14:30:00') - strftime('%s','2004-01-01 00:00:00')
If 2:30pm that you are trying to store is current time then you can shorten it to
strftime('%s','now') - strftime('%s','2004-01-01 00:00:00')