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
Related
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>
According to this answer, Firefox and Chrome accepts the format "YYYY MM DD" while creating a date object.
However, Edge doesn't allow new Date("YYYY MM DD") and wants to be initialized as: new Date("YYYY-MM-DD")
So, should i first check which browser is being used before creating a date object or is there a common pattern by which an date object can be created?
I'm not sure I understant your question because for what I've tried firefox allows you to use new Date("YYYY-MM-DD") and so you could use that and avoid the problem with Edge by using always that, anyway if you're getting an Invalid Date this is my solution by example:
var date;
date = new Date("10 01 01"); //invalid date
if(isNaN(date.getDay())){
date = new Date("2010-01-01")
}
Hope this helps you
As mentioned in the previous answer, new Date("YYYY-MM-DD") should work in Firefox. Test this sample code, for example: http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_string_iso1.
There is a separate issue here related to dates that you may have run into, and that is the aligning of UTC dates with user time zones. This thread has more information about this issue and how to accommodate it: Javascript date object always one day off?
Hope this addresses your concerns!
I was looking for this very specific conversion which I couldnt find anywhere
var d = new Date("2014-12-25T18:30:00+0100");
console.log(d.toString());
the console.log returns an "Invalid Date"
The DateString is returned by the Facebook GraphAPI.
What am I doing wrong? can anyone help?
Thanks in advance
EDIT:
Now that I fixed the API my output is kind of consfusing:
I tried splitting up the String
d.getDay()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes();
it outputs
4.11.114 18:30
why?!
Instead of doing those complicated date functions
d.getDate()+'.'+d.getMonth()+'.'+d.getYear()+' '+d.getHours()+':'+d.getMinutes();
Do yourself a favour and include http://momentjs.com/ in your project. You can then simply take the date from the facebook api and format it with
moment("2014-12-25T18:30:00+0100").format("/* date format */");
See here for formating
SIDENOTE
When formating dates in plain javascript, you will have to add 1 month to your month - january is 0, that's why you get 4.11... instead of 4.12...
Change getYear() to getFullYear()
d.getDay()+'.'+d.getMonth()+'.'+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes();
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/
Hey guys, Im having trouble knowing wat is the correct date format i should put for date.parse() function.
I want to use mm/dd/yyyy in the function like this.
alert("Date: "+Date.parse("11/28/2011"));
//-->Its showing me Date:NaN as output.
also I wanted to know what output should I be getting if I do this following code:
date = Date.parse('12/31/2011' +' UTC');
var minutes=1000*60;
var hours=minutes*60;
var days=hours*24;
var years=days*365;
var y=date/years;
t should give me exactly 42 years but its giving me decimal as well. Y so??
Any help will be appreciated guys.
Thanks
Anand
International (ISO) standard date time format will always work. 2011-12-31
You may also look at this and this
Why would it give you exactly 42 years? Don't forget that there's no such thing as an exact number of milliseconds in a year, due to leap years (and leap seconds, if they're accounted for). Even if there were, you're getting the start of the last day of 2011, which isn't the same as the start of the first day of 2012, which is probably what you were thinking of.
The exact formats supported are implementation-specific as far as I can tell - at least for ECMAScript (which I realise isn't quite the same as JavaScript).