Given a Year, and a day of that year, how can i get the full date? ex: 60/2014 = March First 2014 and 61/2016 = March First 2016
notes: -year and day can be passed as separate parameters.
-result can be a regular Date instance, i just explicitally showed as text to alert for "leap year"
JavaScript has some pretty neat "magic":
var input = "60/2014";
var parts = input.split("/");
var d = new Date(parts[1],0,parts[0]);
That's right. 60th of January. JavaScript will automatically correct this to the first of March.
Related
I need to get the weeks starting and ending date with Javascript/ moment.js
As input i have two values: year and week, which is the isoweek of moment.js
year = '2016'
week = '1'
should give me the 04.01.2016 and 10.01.2016
where the date has the german format moment().format('DD.MM.YYYY');
The solution from your comment will produce an incorrect result on 01.01.2017:
moment([2017,0,1]).year(2017).isoWeek(1).startOf('isoweek').format('DD.MM.YYYY');
// = '04.01.2016'
This one is more stable:
//var year = 2016;
//var week = 1;
var startDate = moment([year, 5, 30]).isoWeek(week).startOf('isoweek');
var endDate = moment(startDate).endOf('isoweek');
startDate.format('DD.MM.YYYY'); // = '04.01.2016'
endDate.format('DD.MM.YYYY'); // = '10.01.2016'
Explanation: if you initialize the moment instance with a date from week 53 of the previous year in conjunction with isoWeek or week, the year component of that moment instance is set to the previous year. All additional moment methods then operate on the "wrong" year.
Therefore use moment([year, 5, 30]) to initialize the moment instance. Any other day after the Jan 3rd works for 2016 too of course, only the few days that belong to week 53 of the previous year cause that problem.
moment([2016]).isoWeek(1).startOf('isoWeek').format('DD.MM.YYYY') // "02.01.2015"
I have a rails-generated date, and a jQuery-generated date.
The rails date prints as such: 2002-10-27
and the jQuery date prints as such: Tue Aug 14 2001 00:00:00 GMT-0500 (CDT)
I want to check if the jQuery date is greater or less than the rails date. But no matter the dates, the jQuery date is always interpreted as larger than the rails date.
Why is that, and how can I successfully compare the two dates?
var year = 2001
var month = 9
month --
var day = 14
var date = new Date(year, month, day);
<% #date = Date.today - 18.years %>
if( date > <%= #date %> ) {
//this code is always executed, no matter what dates I choose
}
UPDATE:
Actually I just figured out the problem is that it only allows dates before 1969. I intended the code to only allow dates over 18 years old. Does anyone know why the difference?
UPDATE 2:
I tested the time output of October 5th, 2000 in my js console and rails consoles, and they give the same first six digits, but the js console adds three zeros.
var year = 2000
var month = 10
month --
var day = 5
var date = new Date(year, month, day);
date.getTime();
=> 970722000000
Date.new(2000,10,5).to_time.to_i
=> 970722000
So it turns out the issue is that the js console prints times in milliseconds, which is why I was getting 973404000000, versus 973404000 in the rails console.
All I had to do was divide the js time by 1000, and comparing the js time to the rails time works perfectly.
var year = 2000
var month = 10
month --
var day = 5
var date = (new Date(year, month, day).getTime() / 1000);
date
=> 970722000
Date.new(2000,10,5).to_time.to_i
=> 970722000
User date Format patterns for Jquery Date, for changing the format of date according to ruby date , or second option is convert ruby date format according to jquery , using strftime.
You might try converting them both to their unix timestamps and comparing those. If you don't care about the hours, and simply the dates, it should work.
var year = 2001
var month = 9
var day = 14
var date = new Date(year, month, day);
<% #date = Date.today - 18.years %>
if ( date.getTime() > <%= #date.to_time.to_i %>) {
// do something
}
I'd use a library like Moment.JS to handle your date parsing needs on the client side. And then send the server something in a standard format like ISO8601 to ensure you don't have any problems in misintrepretation.
Epoch time will work as well, but as you've seen you have to carry the burden of ensuring that they're in the same units.
To compare 2 dates for me u can use moment.js
With ur rails date create a moment date. Do the same with ur jquery date.
U can compare easily the 2 dates now.
If u need help see this post : moment-js-date-time-comparison
I'm trying to validate a uk date using this code:
function ukdate(d) {
var p = new Date(d.split('/')[2], d.split('/')[1] -1, d.split('/')[0]);
if(p.toString() !== 'Invalid Date') {
return p;
}
}
http://jsfiddle.net/GE3xU/1/
so if I try ukdate('31/12/1981') it correctly returns "The Dec 31 1981". However if i try ukdate('12/31/1981') it returns "Tue Jul 12 1983".
Why is this happening? I'm expecting the second test to return invalid date because 31 is not a valid month.
JavaScript is converting your date for you.
In simple examples, you can get the last day of a given month by asking for the 0th day of the following month. Similarly, the "32nd of August" would be corrected to the 1st of September.
Months work similarly. The 13th month of a given year is the 1st month of the next. The 0th month of a year is December of the previous.
31 % 12 = 7, hence July, and floor(31/12) = 2 hence the year being shifted forward by two.
This is intended behaviour for JavaScript.
May I interest you in <input type="date" />? It uses whatever format is defined on the user's computer (ie. it is "locale-aware"), which is already excellent for user experience. On top of that, supporting browsers will render a calendar date picker, especially useful on phones too. Internally, the date is in "standard" YYYY-mm-dd format.
The month value is divided by 12 and added to the year, then the remainder is used as the actual month value.
See the spec
Let ym be y + floor(m /12).
Let mn be m modulo 12.
I'm trying to pass some date information to a date object to format the date based on where the user is hovering in a datepicker.
Using $(this) I'm able to get the day of the given month as a number. Using traversal i'm able to get the month name (February) and the year as a digit (2014).
Is it possible to take these values and apply them to a UTC date string so I can compare with my datepicker?
So far i've got:
var day = $(this).text();
var month = closest_datepicker.find('span.ui-datepicker-month').text();
var year = closest_datepicker.find('span.ui-datepicker-year').text();
which outputs '17 February 2014'
and ... well i'm not sure exactly the next step from here but I want to format this so that I either get the same format as my other dates:
Wed Feb 12 2014 00:00:00 GMT+1100 (EST)
or a millisecond value like this: 1391748657216
You can use a library like moment.js or:
var months = ['January', 'February', 'March']
var day = '17'
var month = 'February';
var year = 2014;
var date = new Date(+year, $.inArray(month, months), +day); // $.inArray() is used instead of Array.indexOf() because of IE compatibility
console.log(date)
I have been using Stack Overflow for a number of months now, but this is my first post.
I require a function to convert a week number and and day of week into a dd/mm/yyyy format.
The date values i have to work with are in the format day/weekNumber. So for example: 3/43 converts to Wednesday 24 October 20XX. The year value will be the current year.
The day value starts at 1 (Monday).
I have found lots of functions on the internet (such as this, this and this). Some work with ISO 8601 dates, which i do not think will work for me. And i have not yet found one that works for me.
Thanks in advance,
This solution does require an extra library to be added, but I think it is really worth it. It is a momentjs library for manipulating dates and time. It is actively maintained and has a great documentation. Once you get the values for day and weekNumber (in our case 3 and 43), you should do as follows:
function formatInput(day, weekNumber){
var currentDate = moment(new Date()); // initialize moment to a current date
currentDate.startOf('year'); // set to Jan 1 12:00:00.000 pm this year
currentDate.add('w',weekNumber - 1); // add number of weeks to the beginning of the year (-1 because we are now at the 1st week)
currentDate.day(day); // set the day to the specified day, Monday being 1, Sunday 7
alert(currentDate.format("dddd, MMMM Do YYYY")); // return the formatted date string
return currentDate.format("dddd, MMMM Do YYYY");
}
I think this library might be useful to you later on and there are plenty of possibilities regarding date and time manipulation, as well as formatting options. There is also a great documentation written for momentjs.
So assuming you have the values of 3 and 43 separately, you can just do some simple maths on the first day of the current year:
Get 1st January Current Year
Add (43 * 7 + 3)
Something like this maybe:
var currentDate = new Date();
var startOfYear = new Date(currentDate.getFullYear(), 0, 1);//note: months start at 0
var daysToAdd = (43 * 7) + 3;
//add days
startOfYear.setDate(startOfYear.getDate() + daysToAdd);
Here is an example
EDIT
On second thoughts, I think I was wrong with your requirements. It seems you require a specific day of the week. Check this out for a better solution.
The problem is that it all depends on your definition of a week. This year starts on a sunday, so does that mean that 02/01/2012 (the first monday of this year) is the start of the second week?
My latest example will first find the start of the specified week, and then find the next occurrence of the specified day
According to ISO when dealing with week dates, the week starts on Monday and the first week of the year is the one that contains the first Thursday of the year. So for 2012, the first week started on Monday, 2 January and the first week of 2013 will start on Monday, 31 December 2012.
So if 3/43 is the third day of the 43rd week (which is the ISO date 2012-W43-3), then it can be converted it to a date object using:
function customWeekDateToDate(s) {
var d, n;
var bits = s.split('/');
// Calculate Monday of first week of year this year
d = new Date();
d = new Date(d.getFullYear(),0,1); // 1 jan this year
n = d.getDay();
d.setDate(d.getDate() + (-1 * n +( n<5? 1 : 8)));
// Add days
d.setDate(d.getDate() + --bits[0] + --bits[1] * 7);
return d;
}
console.log(customWeekDateToDate('3/43')); // 01 2012-10-24
Note that this uses dates, otherwise daylight saving changeovers may result in the wrong date.