moment JS returns wrong date - javascript

I'm passing UTC timestamp to moment js and timezone in order to get back the real date.
This is how I'm doing it:
formatDate: function(dateTime, timezone) {
var format = 'D-MMM-YYYY';
return moment.utc(dateTime, format).tz(timezone).format(format);
}
So I would pass on something like formatDate(1399922165, 'America/Los_Angeles'); and it returns 12-Jan-9992 instead of 12-May-2014.
If instead I do it like this:
moment(dateTime).tz(timezone).format(format);
Then it returns 16-Jan-1970.
Thanks to Ian, this ended up being the solution.
moment.unix(dateTime).tz(timezone).format(format);
Any ideas?

Thanks to Ian, this ended up being the solution.
moment.unix(dateTime).tz(timezone).format(format);
I was trying moment.utc() instead of moment.unix().
The strange results came from moment.utc(datetime, format) expecting datetime to match format. However, it's important to note that moment.utc(datetime) still returns 1970 year result so it still wouldn't have returned the desired result even without the format.

Related

moment format using i18n

I am replacing moment by date-fns and I found this code, that I do not understand the moment part (what moment does). I understand the map function.
this.events[0].dates.map(date => moment(date).format(this.$i18n.locale))
I create an environment to test the moment function, but the result I got was weird and now I am completely lost.
I passed to
moment()
const startDate = "2021-07-26";
const actualDay = new Date();
const timeStamp = 1624988893000;
to figure out what this function does, and all time the result was something like this MOMENT TEST 610-63
I think there is something wrong with that function, but I am doubting, maybe too much.
What momentjs is doing is clearly described here:
https://momentjs.com/
moment is awaiting a date. So
moment(new Date()).format()
https://jsfiddle.net/95dbLmkf/
Is giving you current date and time in ISO format.
I do not know what i18n.locale contains but I guess it is not a valid date format string as required by momentjs format command.
Maybe you just console.log the value of i18n.locale to see what it is passing through the format function.

proper way to format the date depending on scenario

I have a scenario where I can have a date coming as a string. It can be formatted as either:
2021-05-29
29-05-2021
Im using dateFns to parse the strings, but when the string is in the format 2021-05-29, it cant parse the string, which is why, when it has the format of 2021-05-29, it needs to be converted into 29-05-2021...
The parse() function from dateFns what actually what I needed to parse it to the correct format, but it doenst seem to work when passing 2021-05-29
return parse(date, 'mm-dd-yyyy', new Date());
Any ideas what how to handle this?
You could try reversing the date,
date = date.split(“-“).reverse().join(“-“)

To convert date format to the time stamp

How to calculate the start of the day.
I calculate by
=> moment().startOf('day').toISOString()
it returns
2019-09-12T00:00:00.000
But I want to in this format "2019-09-12 00:00:00.000"
How can we get,Please help me.
var formatedDate= moment().startOf('day').format('YYYY-MM-DD HH:mm:ss,sss');
Instead of using .toISOString() method you can simply use format().
As per momentjs doc here
.toISOString() returns a timestamp in UTC, even if the moment in
question is in local mode.
Use it like this:
moment().startOf('day').format('YYYY-MM-DD hh:mm:ss:SSS')
Hope this helps happy coding!!!

moment .toJSON returns "0NaN-NaN-NaNTNaN:NaN:NaN.NaNZ"

I'm using moment.js and sometimes when calling:
myMomentVariable.toJSON()
I get back:
"0NaN-NaN-NaNTNaN:NaN:NaN.NaNZ"
instead of the ISO8601 string.
I'm sure I'm doing something stupid with my myMomentVariable somewhere in my code that causes this, but I can't find where... Any tips for what might cause this error?
Need code, but I guess myMomentVariable doesn't represent a valid date...
check your variable before put it in moment function.
maybe you have to convert your date from string to date.
like let mydate = Date(myStringdate);

Convert from string with milliseconds to date object Javascript

I got this problem when dealing with date time conversion. I have timestamp data from postgreSQL database with format like this one
"2011-04-04 19:27:39.92034"
In order to display it in highcharts, I have to convert it to date or time object. Without milliseconds, I easily convert it with Date.js
But milliseconds can't be handled with that library. I tried also with Date.parse but always got NaN.
Any solution for this problem? Thank you
JS built in Date class should be able to handle this, and getTime() can return milliseconds since start 1970 (UNIX time). Watch out for time zone issues though; the constructor may interpret the date/time as being local, but getTime()'s milliseconds since 1970 may be in UTC, baking in a conversion that is difficult to remove.
new Date("2011-04-04 19:27:39.92034").getTime()
1301941659920
Many ways to Rome. The given code will return '(datestr=) 2011-4-4 19:27:39.92'. Is that what you look for?
var darr = '2011-04-04 19:27:39.92034'.split('.')
, dat=new Date(darr[0])
, datestr = '';
dat.setMilliseconds(Math.round(darr[1]/1000));
datestr = [ [dat.getFullYear(),dat.getMonth()+1,dat.getDate()].join('-')
,' ',
[dat.getHours(),dat.getMinutes(),dat.getSeconds()].join(':')
,'.',
dat.getMilliseconds()
].join('');
Can't you just cut of the last 6 chars of that string? You might then round the miliseconds and eventually add a second to you time object.
This is simpler and in one line:
new Date('01/09/2015 06:16:14.123'.split(".")[0])

Categories

Resources