moment format using i18n - javascript

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.

Related

Adding days to a moment object is return same moment object

I've a moment object to which I'm trying add some days.
However, it is returning the same moment object as a result.
But, if I simply try to do it on the current date, it works fine.
Also, note that, I always receive a moment object to which I need to add the days.
Code:
const someDate = moment('22-03-2020');
console.log(someDate.add(5, 'days');
someDate is something I receive from the server and is always a moment object.
How do I fix this?
You should specify what format you used, like so:
const someDate = moment('22-03-2020', 'DD-MM-YYYY')
const newDate = someDate.add(5, 'days')
console.log(newDate)
To format a moment object, you just have to add .format() like so:
console.log(newDate.format())
"Deprecation warning in moment js - Not in a recognized ISO format"
if you provide not valid ISO format for more check here information
Deprecation warning in moment js - Not in a recognized ISO format
// Recommended format: YYYY/MM/DD
const someDate = moment('2020-03-22');
console.log(someDate.add(5, 'days').format("YYYY-MM-DD"));
Test here

Momentjs get date and set format

for a weight tracking project i want to get a date from a user in his specific format eg:"05-12-19" , and i want to format it with momentjs to the standard javascript format
This code below is what i tried and think is the nearest to the result i want:
let newDate = moment().format("05-12-19","DD-MM-YYYY");
console.log(newDate); //05-12-19
the result that i was expecting is 05-12-2019 but got something different take a look here (trying to meet stack-overflows quality standards lol)
To create your date, something like this:
let newDate = moment("05-12-19","DD-MM-YY");
console.log(newDate.toDate());
to output your desired format
let newDateStr = moment("05-12-19","DD-MM-YY").format("DD-MM-YYYY");
console.log(newDateStr);
after looking for a while i found a similair answer here.
it wasn't exactly what i was looking for so i'll post here my complete answer:
let newDate = moment("05-12-19", "DD-MM-YY").format("DD-MM-YYYY");
console.log(newDate);
in the moment function the first argument is my date, the second argument is the format of this date, because momentjs doesn't know this format. in the format function i enter the date i want it to format to.

datejs overwriting Date in javascript

I am using date js to quickly parse any string into a date and it is working perfectly. However I need to also parse a timestamp.
var temp_string = "1484120122526";
var date = new Date(temp_string);
It gives back
NaN –
Regular javascript Date object does this, but I can't find a way for datejs to do it. And since it overwrites the Date object, I am stuck. Can datejs parse timestamps? or is there a way for me to call new Date() and reference the original date object?
Even though dateJS does indeed overwrite the default js Date class, the fault was on my side, I did not see in the documentation the fact that timestamp has to be an int, as I was looking everywhere in the dateJS documentation not the Date one.
so the code should be:
var temp_int = 1484120122526;
var date = new Date(temp_int);

moment JS returns wrong date

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.

Moment.js transform to date object

Using Moment.js I can't transform a correct moment object to a date object with timezones. I can't get the correct date.
Example:
var oldDate = new Date(),
momentObj = moment(oldDate).tz("MST7MDT"),
newDate = momentObj.toDate();
console.log("start date " + oldDate)
console.log("Format from moment with offset " + momentObj.format())
console.log("Format from moment without offset " + momentObj.utc().format())
console.log("(Date object) Time with offset " + newDate)
console.log("(Date object) Time without offset "+ moment.utc(newDate).toDate())
Use this to transform a moment object into a date object:
From http://momentjs.com/docs/#/displaying/as-javascript-date/
moment().toDate();
Yields:
Tue Nov 04 2014 14:04:01 GMT-0600 (CST)
As long as you have initialized moment-timezone with the data for the zones you want, your code works as expected.
You are correctly converting the moment to the time zone, which is reflected in the second line of output from momentObj.format().
Switching to UTC doesn't just drop the offset, it changes back to the UTC time zone. If you're going to do that, you don't need the original .tz() call at all. You could just do moment.utc().
Perhaps you are just trying to change the output format string? If so, just specify the parameters you want to the format method:
momentObj.format("YYYY-MM-DD HH:mm:ss")
Regarding the last to lines of your code - when you go back to a Date object using toDate(), you are giving up the behavior of moment.js and going back to JavaScript's behavior. A JavaScript Date object will always be printed in the local time zone of the computer it's running on. There's nothing moment.js can do about that.
A couple of other little things:
While the moment constructor can take a Date, it is usually best to not use one. For "now", don't use moment(new Date()). Instead, just use moment(). Both will work but it's unnecessarily redundant. If you are parsing from a string, pass that string directly into moment. Don't try to parse it to a Date first. You will find moment's parser to be much more reliable.
Time Zones like MST7MDT are there for backwards compatibility reasons. They stem from POSIX style time zones, and only a few of them are in the TZDB data. Unless absolutely necessary, you should use a key such as America/Denver.
.toDate did not really work for me, So, Here is what i did :
futureStartAtDate = new Date(moment().locale("en").add(1, 'd').format("MMM DD, YYYY HH:MM"))
hope this helps
Since momentjs has no control over javascript date object I found a work around to this.
const currentTime = new Date();
const convertTime = moment(currentTime).tz(timezone).format("YYYY-MM-DD HH:mm:ss");
const convertTimeObject = new Date(convertTime);
This will give you a javascript date object with the converted time
The question is a little obscure. I ll do my best to explain this. First you should understand how to use moment-timezone. According to this answer here TypeError: moment().tz is not a function, you have to import moment from moment-timezone instead of the default moment (ofcourse you will have to npm install moment-timezone first!). For the sake of clarity,
const moment=require('moment-timezone')//import from moment-timezone
Now in order to use the timezone feature, use moment.tz("date_string/moment()","time_zone") (visit https://momentjs.com/timezone/ for more details). This function will return a moment object with a particular time zone. For the sake of clarity,
var newYork= moment.tz("2014-06-01 12:00", "America/New_York");/*this code will consider NewYork as the timezone.*/
Now when you try to convert newYork (the moment object) with moment's toDate() (ISO 8601 format conversion) you will get the time of Greenwich,UK. For more details, go through this article https://www.nhc.noaa.gov/aboututc.shtml, about UTC. However if you just want your local time in this format (New York time, according to this example), just add the method .utc(true) ,with the arg true, to your moment object. For the sake of clarity,
newYork.toDate()//will give you the Greenwich ,UK, time.
newYork.utc(true).toDate()//will give you the local time. according to the moment.tz method arg we specified above, it is 12:00.you can ofcourse change this by using moment()
In short, moment.tz considers the time zone you specify and compares your local time with the time in Greenwich to give you a result. I hope this was useful.
To convert any date, for example utc:
moment( moment().utc().format( "YYYY-MM-DD HH:mm:ss" )).toDate()
let dateVar = moment('any date value');
let newDateVar = dateVar.utc().format();
nice and clean!!!!
I needed to have timezone information in my date string. I was originally using moment.tz(dateStr, 'America/New_York').toString(); but then I started getting errors about feeding that string back into moment.
I tried the moment.tz(dateStr, 'America/New_York').toDate(); but then I lost timezone information which I needed.
The only solution that returned a usable date string with timezone that could be fed back into moment was moment.tz(dateStr, 'America/New_York').format();
try (without format step)
new Date(moment())
var d = moment.tz("2019-04-15 12:00", "America/New_York");
console.log( new Date(d) );
console.log( new Date(moment()) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data.min.js"></script>
moment has updated the js lib as of 06/2018.
var newYork = moment.tz("2014-06-01 12:00", "America/New_York");
var losAngeles = newYork.clone().tz("America/Los_Angeles");
var london = newYork.clone().tz("Europe/London");
newYork.format(); // 2014-06-01T12:00:00-04:00
losAngeles.format(); // 2014-06-01T09:00:00-07:00
london.format(); // 2014-06-01T17:00:00+01:00
if you have freedom to use Angular5+, then better use datePipe feature there than the timezone function here. I have to use moment.js because my project limits to Angular2 only.
new Date(moment()) - could give error while exporting the data column in excel
use
moment.toDate() - doesn't give error or make exported file corrupt

Categories

Resources