Moment not correctly parsing time in milliseconds - datetime

I have the following code:
job_created_at_timestamp = moment(job_created_at_timestamp_milliseconds).format('YYYY-MM-DD hh:MM:SS A');
Where job_created_at_timestamp_milliseconds is a timestamp in milliseconds, for example: 1463422849081.
Moment is not correctly converting these timestamps, for example 1463422849081 is converted to 2016-05-16 02:05:08 PM, when it should be 2016-05-16 02:20:49 PM.
I also tried to 'reconvert' the timestamp into a unix time:
moment.unix(job_created_at_timestamp_milliseconds).format('YYYY-MM-DD hh:MM:SS A');
but that timestamp is less correct.
I verified that the milliseconds are correct using this website. Ideas?

The timestamp format was wrong:
moment(job_created_at_timestamp_milliseconds).format('YYYY-MM-DD hh:mm:ss A');

Related

How to convert the UTC date into local date in Moment.js?

I have a date in this format "2020-12-16T02:48:00" that came from the server. How can I convert this into local date and time? I tried some code but couldn't succeed.
Below is the attempt that I had made in angular after receiving date from the server.
response.data.map(date=>{
var centralDate = moment( date).zone("-06:00");
date = moment(centralDate).local().format('YYYY-MM-DD hh:mm:ss');
})
If indeed the value is in UTC (as per the title of your question), and it looks like "2020-12-16T02:48:00", and you want to convert it to local time, then you should do the following:
moment.utc(date).local().format('YYYY-MM-DD HH:mm:ss');
That does the following:
Parses the input in terms of UTC
Converts it to local time
Formats it as a string in the given format
Note also that you had hh in your original format. That is for hours in a 12-hour time format and thus you shouldn't use it without also using either A or a to indicate AM/PM or am/pm. Otherwise HH is for hours in a 24-hour time format.
If your issue is that the timezone doesn't change you can resolve using utcOffset (https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/09-utc-offset/) in this way:
response.data.map(date=>{
date = moment( date).utcOffset(-360);
})
Where 360 is the conversion fo the hours in minutes
var d= new Date();
d = new Date(d+ "Z")
I am not an expert in angular but I guess the trouble in your date is the word “T”. May be using string removal function you can remove the word “T” and then it becomes a proper date time value?

MomentJS - How to get 00:01:00.02 format from seconds.milliseconds value?

My value is 60.02 so how can I transform it in hours:minutes:seconds.milliSeconds format to get 00:01:00.02 format using the momentJS library?
You could use moment duration to format your time string
let seconds = '60.02';
moment.utc(moment.duration({
seconds: seconds
}).asMilliseconds()).format('HH:mm:ss:SS');
For the whole moment duration docs see here

How to get a date in milliseconds with moment.js

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();

How to convert the following time format (2014-10-05T22:25:00-07:00) into required date format in javascript

I want to convert the given time into required time format in javascript using moment.js
moment('2014-10-05T22:25:00-07:00').format('DD-MM-YYYY HH:ss Z');
I am tried the above code to get result "06-10-2014 3.30" but it returns "06-10-2014 10:00 +05:30"
The time you are passing in is
2014-10-05T22:25:00-07:00
which is 5:25am on the 6th of October, GMT. You're then outputting it using the timezone UTC+05:30, which should give:
06-10-2014 10:55:00 +05:30
Unfortunately, you've missed mm from your date string, so you are only seeing:
06-10-2014 10:00 +05:30
Simply change your date format to DD-MM-YYYY HH:mm:ss Z to correct this.

How to convert time to seconds with SQLite

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')

Resources