Description of the Issue and Steps to Reproduce:
Receive user input as 3/3 var response
Parse into a date variable var date = moment(new Date(response))
Doing a console.log of date gives moment("2001-03-03T00:00:00.000")
The year defaults to 2001. Since the user may input the date in their own format, I didn't want to add in a format as I wouldn't know what format they might want to enter.
After looking around, I found some Moment github issues on this (#635, #912) which mentioned that the issue was resolved, but I am still getting the default year of 2001.
I also found a suggestion to set the year as this year if left unspecified:
if (date.year() === 2001) {
date.year() = moment().year();
}
This works, but feels like a dirty solution. Any ideas what I can do instead?
Thanks in advance!
Current Environment
Node.js v8.9.4
Moment.js v2.20.1
VS code v1.19.3
MS Bot SDK v3.14.0
p/s Still pretty new to the stackoverflow/ github issues, and not to sure where I should have posted instead. Please let me know if you need more information!
Use moment with a format string to ensure the format is proper. You might want to force the user to provide the year or use a date picker. The machine can only do so much
console.log(moment('3/3', 'MM/dd').toString());
// or
console.log(moment('3/3', 'dd/MM').toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Related
DayJS format() incorrectly converts almost any historical date. In this example I try to format the year 2 AD (from user input). The output is the year 1902 AD, which is absolutely wrong. Now I am wondering if I got this method wrong or it is actually a bug and of course how to solve this anyway.
Have created a Codepen for this
const formatedDate = dayjs('0002-05-01', 'YYYY-MM-DD').format('YYYY-MM-DD')
console.log(formatedDate)
//Output: "1902-05-01"
There is an open issue somebody solved this with setting new Date().setFullYear(2). I hope it helps
For example, I want to set a variable to 4 pm, where it will be 4 pm for everyone. I'm sure there will be some sort of solution for that, a real-world example is, you set a calendar event for 4 pm, and whoever is in that event will also receive the reminder at 4 pm of their timezones, how do we achieve it?
I've been googling and looking at the moment js docs but I couldn't figure it out yet, any thoughts?
I've been working on this jsFiddle but I don't think this is the path, https://jsfiddle.net/wmoreiradev/y70Lnmzb/7/
const utcOffset = new Date().getTimezoneOffset();
const date = moment('2020-06-24T23:45:00.000Z').utcOffset(utcOffset).toDate();
What you're trying to achieve is ambiguous, as 4 PM in all timezone is just simply doesn't exist at the same time.
BUT, for any reason, if you want to do it, you can do it, just pass the date as a simple string out it in the simple string. From simple string, I mean not a date object.
moment("4:00PM", "hh:mmA").format();
It will always give you 4:00PM in all the timezone.
Using momentJs, is possible to get the first day of the week (Monday(1), Sunday(7)...) based on Locale without creating a new moment?
I know I can access what is the first day of the week for the current locale with:
moment.locale('uk');
moment().startOf('week').isoWeekday(); //Returns 1
moment.locale('en');
moment().startOf('week').isoWeekday(); //Returns 7
But I think it's a bit ugly...
Creating a momentjs object.
Going to the first date of the week.
Resolving the weekDay.
Any better idea?
Thx!
This question has a proper answer in momentjs's current API:
moment.localeData('en-us').firstDayOfWeek();
As OP asked - no instance of moment() needed, also no ugliness of going to "start of", just plain simple use of their localeData.
Note, that it might be required to download the moment+locale file which is significantly larger (44kb) than moment only (about 12kb).
Seems to be the case from version 2.2.0, more info can be found on their docs:
http://momentjs.com/docs/#/i18n/locale-data/
Looks to me that you wanted to get a locale aware start of week (startOf('week')) and return its value as an isoWeekday, not the date or anything? In that case your question is the answer.
moment().startOf('week').isoWeekday();
It looks like you can just do moment().locale('us').weekday(0) as of version 2.1.0
http://momentjs.com/docs/#/get-set/weekday/
I'm using moment-with-locales.min.js for date manipulation and I need to format as the user leaves the textboxes. Because locales are an issue, I'm trying to use moment to do the formatting. However, I'm running into a problem and I'm not sure if I'm doing it wrong or what.
If the user types in something like '2/2/12' and I try to do moment('2/2/12', 'l'), using the 'l' for short date based on locale, it formats it into '2/2/0012'. That in itself seems broken.
If I try to format with moment('2/2/12', 'M/d/yyyy'), as seen in the JSFiddle below, it changes it to '2/1/2014'. It always bumps it down to the first day of the month and makes it the current year.
Here's the JSFiddle I was using.
moment.locale('en-US');
var parsed = moment('2/2/12', 'M/d/yyyy');
if (moment(parsed).isValid()) {
var d = new moment(parsed, 'l');
alert('Pass: ' + d.format('l'));
} else {
alert('Fail: ' + parsed);
}
I'd appreciate and help.
You should use "D" ("d" is day of week).
As for the year, according to the docs, «Two digit year parser by default assumes years above 68 to be in the 1900's and below in the 2000's.» so I'm guessing (and experimentation seems to confirm it) that if you use the 4 digit format ("YYYY") it'll assume you are passing in the year 12 and not 2012. If you use "YY" it'll print 2012 correctly.
So, to summarize, the format "M/D/YY" should do what you want.
Yesterday I managed to solve this, then lost a day's work due to the death of a HD. Now I cannot remember what I did to fix it, but I know it can be done.
Input: string date in the format 'm/d/yy', eg '12/25/10', or '4/1/10' (1st April)
Output - Date object
I'm working with date.js and date.format.js so have Date.fromString() and Date.format() avaiable. But trying multiple combinations is not giving me what I need.
IF the date were 'mm/dd/yy' then it's simple. But I'm using jquery.datepicker.js which outputs in 'm/d/yy' and I don't want to change this much I know this conversion can be done.
After a 22 hour day... I need help.
Thanks.
Mark...
use the datejs Date.parse function