

Is there a way to convert the moment object into a plain string like 'Tue 23:59:59 GMT+0800 (HKT)' I tried toString() and toUTCString() and it doesn't work.

But in IE it is having issues, from this I understood we can apply one dateformat on the given date, If we want second date format, it should be apply on the fresh date not on the first date format result.Īnd also observe that for first time applying 'MM-DD-YYYY' and next 'MM-DD-YY' is working in IE. To create a moment from a Unix timestamp ( seconds since the Unix Epoch), use moment.unix (Number). When I do it with my react native mobile app, it passes the date as a moment object and it doesn't get converted to a string and it doesn't work.

Then, when I use another format on "06/28/20": startDate = moment(startDate ).format('MM-DD-YYYY') Result: 06-28-1920, in google chrome and firefox browsers it gives correct date on second attempt as: 06-28-2020. The result is "Mon 00:00:00 GMT+0530 (India Standard Time)", What happening is it retains only the year part :20 as "06/28/20", after If I run the statement : Var startDate = moment(new Date()).format('MM/DD/YY') Result: 06/28/20 I am using moment.js format function on a current date as (today is 29-06-2020) May be this helps some one who are looking for multiple date formats one after the other by willingly or unexpectedly. Stack Trace: at makeDateFromStringAndFormat ()Īt _personal.rendered ()

I should also mention that I am using a pre-packaged version of Moment.js, packaged for Meteor.js Object has no method 'replace' : The Exact error from the console But is not, this test with a string in Format 1 is also true. Format 1 > 25.03.20 (DD.MM.YY) Format 2 > (YYYY-MM-DD) I thought for option Nr 1 this: moment (dateString, 'YYYY-MM-DD HH:mm').isValid () would be false. Which errors and says there is no such method called replace? Am I approaching this in the wrong way? Im getting dates as strings in two different date formats: Like. I tried to do it using this method, moment(testDate,'mm/dd/yyyy') I would like to use Moment.js get it in this format mm/dd/yyyy : for display. var ts = moment(" 9:00", "M/D/YYYY H:mm").I have a string in this format: var testDate = "Fri 19:08:55 GMT-0500 (CDT)" I am doing this: moment(Mon 0, 11:00 AM, 'dd-mm-yyyy hh:mm'). The problem here is that I am unable to get AM or PM from the date time string. unix() to return the timestamp in whole seconds, and moment.unix(ts) to parse it back to a moment. I have a string as Mon 0, 11:00 AM/PM and I have to convert this into a string like 11:00 AM/PM using moment js. Since the input value is interpreted in terms of local time, you will get a different value for ts if you are in a different time zone.Īlso note that if you really do want to work with whole seconds (possibly losing precision), moment has methods for that as well. So if you replace moment.parseTwoDigitYear in you code, you will have 2069 instead of 1969. This can be changed by replacing the moment.parseTwoDigitYear method. On my machine, in the US Pacific time zone, it results in: From moment docs: By default, two digit years above 68 are assumed to be in the 1900's and years 68 or below are assumed to be in the 2000's. Putting it all together: var ts = moment(" 9:00", "M/D/YYYY H:mm").valueOf() Īlert("Values are: ts = " + ts + ", s = " + s) To answer your questions in comments, No - you don't need to call. Again, you may want to pass a format specifier. If that's not what you expected, you should use the. If you are near to the date, it will return a value like "Today 9:00 AM". (This is also why you get the deprecation warning in the console.) Instead, provide a format string that matches the expected input, such as: moment(" 9:00", "M/D/YYYY H:mm")
MOMENT FORMAT TO DATE STRING CODE
That isn't a good idea, as values like could be interpreted as either February 1st or as January 2nd, depending on the locale of where the code is running. You're parsing a string without providing a format specifier. unix()*1000 would also work, but it would result in a loss of precision. unix() returns Unix Time in whole seconds, but the default moment constructor accepts a timestamp in milliseconds. You probably meant "Unix Time", which is often erroneously called "Epoch Time". You can't convert an arbitrary "date string to epoch". The "Unix Epoch" is Midnight, January 1st 1970 UTC. "Epoch" refers to the starting point of something.
