i would like to create a url based on the year and the week number in order to redirect to a document which the name changes every week :
the document is stored at this place : server.com/kw38_2013.pdf (which kw38 mean week 38)
Does someone know a javascript for that ?
Regards
Emmanuel
It will be very easy to get year from a given date object in javascript. See Date Object Reference.
Whereas, Getting the week number from the given date will be tricky. Here is a similar question to get the week number from given date. See Get week of year in JavaScript.
Hope it helps.
See here to get the week number.
You get the year by creating a Date object : new Date().getFullYear()
Related
I have one problem. Can you tell me how to check does it day in the current week?
I am working on some service for a weather forecast. I have a current day but must check does it in the current week. Only what I know is that I must use 'isSame' function from Moment JS.
This is my line of code.
if(this.conversation.payload.grain==="week" && moment().startOf('week').isSame(this.conversation.payload.forecastTime))
"forecastTime" is a current day. However, the condition is not good and does not enter the if loop.
Thank you!
This is assuming your forecastedDate is an actual javascript date or a moment object.
The isSame function also takes in a granularity parameter.
Just add the 'week' parameter to the isSame method like so:
if(this.conversation.payload.grain==="week" &&
moment().startOf("week").isSame(this.conversation.payload.forecastTime, "week"))
To get the day of the week is easily done with the momentjs library.
This will give you a day of the week based on the locale:
var dayOfWeek = moment(this.conversation.payload.forecastTime).weekday()
For example, if the locale uses Monday as the first day of the week then 0 = Monday and 6 = Sunday. Please keep in mind that the value you recieve will change based on the current locale.
If you don't want the value to change based on locale but always want to receive a Monday-Sunday value use isoWeekday():
var dayOfWeek = moment(this.conversation.payload.forecastTime).isoWeekday()
This will give you an integer 1-7. 1 = Monday, 7 = Sunday.
So, for example, if the above code returned 4, you would know that it was Thursday.
For more details on the weekday(), isoWeekday, and day() functions, check out the momentjs docs.
This question already has an answer here:
How to get the integer value of month from moment.js
(1 answer)
Closed 3 years ago.
EDIT: NB: As pointed out in the accepted answer from Zohaib Ijaz month gives the 0-11 value for the month but the second problem is day. day gives a numeric representation of day of the week ie 0 = sunday, 1 = tuesday. If you want to get the 1-31 you need date.
OK I think this must be a super dumb question but I just cannot see it. Today is 11th November 2019
var day = moment().get('day');
var month = moment().get('month');
var year = moment().get('year');
var dateSet = month+"/"+day+"/"+year;
console.log(dateSet);
and I get back: 10/01/2019!
I tried the functional getter of var day = moment().day(); etc. and same again.
I am trying to get today's date and the date in three months times for a datepicker. Greatful for an explanation of this and extremely grateful for a pointer as how to do the +3 months which I tried moment().plus(3,"months") which did not work.
I am extremely tired but I am pretty sure at one stage this was giving the right date. What could possibly have changed?
moment().month() or moment().get('month') will return month from 0 as January to 11 as December. So if you want to create date in MM/DD/YYYY format, user moment().format(format_string). Or add 1 in month while creating your date string. I would suggest to use format and go through moment docs first so you have better idea what it provides out of the box.
See docs
https://momentjs.com/docs/#/get-set/get/
moment().format('MM/DD/YYYY')
I use the jQuery UI Month Picker Plugin to add a month picker in my app.
I don't find the way to get the value of the selected month in Javascript ?
I see in documentation you could get the month as a number! simply create a array with 12 months and use the method GetSelectedMonth() and use that number to get the relevant month from the array.
var arr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
arr[GetSelectedMonth()-1] //-1 is used since array starts from 0
I am sharing the link you could get the month as a number! simply create a array with 12 months and use that number to get the relevant month from the array.
"http://jsfiddle.net/tmnasim/JLydp/"
how to display the date in this below format using jQuery.
Thursday, January 08, 2013
I saw some plugins but wondering if there is a way without using any plugin.
Please advise if there is a straightforward answer using JavaScript, that's fine too.
The simplest answer is to use:
date.toLocaleDateString()
But, it will use the locale defined by the user's system. The American/English locale fitting your desired output. (I'm not sure about other locales and how they format dates).
So, if you want the date string to always be in that format, this will not be the best answer for you. But, if you want the date to match the user's locale, this answer is the simplest and most correct. :)
http://jsfiddle.net/SyjpS/
var date = new Date();
console.log(date.toLocaleDateString()); // Tuesday, January 08, 2013 (on my machine)
EDIT — If you're asking how to change the calendar so that today is Thursday instead of Tuesday, you may need to talk to Caesar about adjusting the calendar realignment. For this, you'll need a time machine. I suggest that you seek out the Doctor, but he may not be willing to change history willy nilly.
Here's a quick/simple example of what you're asking for:
EDIT - I've update the code for reuse and include the day 0 padding change.
var d = new Date();
console.log(formatDate(d));
function formatDate(d){
var months = ["Januaray", "February", "March"]; //you would need to include the rest
var days = ["Sunday", "Monday", "Tuesday"];//you would need to include the rest
return days[d.getDay()] + ", " + months[d.getMonth()] + " " + (d.getDate() < 10 ? "0" + d.getDate() : d.getDate()) + ", " + d.getFullYear();
}
Output for today: Tuesday, Januaray 08, 2013
EXAMPLE
Simply use DateJS not to reinvent the wheel.
You may read the API documentation here:
http://code.google.com/p/datejs/wiki/APIDocumentation
The date methods allow you to retrieve all of the different parts of the date and time as numerical values. In the case of the month of the year and the day of the week, the number that is provided is one less than you would normally expect. The reason for this is that the most common use for these values is to use it to look up the name of the month or day from an array and as arrays in JavaScript are numbered from zero, providing the numbers like that reduces the amount of code needed to do the name lookups.
We can go one better than just doing this lookup using the retrieved values though. What we can do is to add extra methods to the Date object to allow us to retrieve the month and day names whenever we want the exact same way that we retrieve any of the other information about the date.
The probable reason why the following methods are not built into the JavaScript language itself is that they are language dependent and need to have different values substituted into the code depending on the language that you want to display the month and day in. For the purpose of showing you how to code this I am going to use the Emglish names for the months and days. If you want to display dates in a different language you will need to substitute the names from that language for their English equivalents.
What we are going to do is to add getMonthName() and getDayName() methods to all our dates so that we can get the month name or day name by calling these new methods directly instead of having to call getMonth() or getDay() and then do an array lookup of the corresponding name. What we are actually doing is building the required array lookups into the new methods so that we can automatically get the correct names for any of our date objects simply by calling the appropriate method.
We don't neeed all that much code to do this. All you need to do to add the getMonthName() and getDayName() methods to all of the date objects that you use is to add the following short piece of code to the very top of the javaScript code attached to the head of your page. Any subsequent processing of date objects will then be able to use these methods.
Date.prototype.getMonthName = function() {
var m = ['January','February','March','April','May','June','July',
'August','September','October','November','December'];
return m[this.getMonth()];
}
Date.prototype.getDayName = function() {
var d = ['Sunday','Monday','Tuesday','Wednesday',
'Thursday','Friday','Saturday'];
return d[this.getDay()];
}
So now with that in place we can display today's date on the page using those new methods in addition to the existing ones. For example we might use the following to get the full date and display it using an alert:
var today = new Date;
alert((today.getDayName() + ', ' + today.getDate() + ' ' + today.getMonthName() + ', ' + today.getFullYear());
Alternatively, we can just retrieve the current month June and day of the week Sunday and use them however we want just the same as for any of the other parts of the date.
function disp() {
var today = new Date;
document.getElementById('mth').innerHTML = today.getMonthName();
document.getElementById('dow').innerHTML = today.getDayName();
}
Okay, I have heard about it but I can confirm now that the Javascript Date functionality is a disaster zone. And I have created a monster out of it. I have this Program :
A JSON object contains list of holiday dates and its respective label.
I need to find out the date of 5 business days from today (excluding saturday, sunday and holiday if any which is contained in the JSON object.) Good stuff so far. Then this 5 business days' date is going to be devoured by the jquery calender as a default selected date which is not included in the fiddle as it is irrelevant. (Note: the start date on the calender is tommorow's date) Good stuff again. THEN, comes this part: If it is before noon today, I can select tommorow else start date is day after tommorow. I'm elaborating this because it is included in this fiddle.
So the problem is multiple initialization of the function which handles above functionality is not producing consistent result. It was calculating 5 business days on my system, but when i made this fiddle, it is calculating 4. The date of "5th" business days is incremental by 1 on each call.
http://jsfiddle.net/xXQ7j/27/
Anyone!
Your problem is probably caused by timezone issues.
Whenever possible you should use new Date(y, m, d) to create a date object, rather than supplying a string. In particular, I've found that you get a date relative to 00:00 UTC if you specify a string in format yyyy-mm-dd but one relative to local midnight if you use yyyy/mm/dd.
In any event, I would suggest a different approach:
convert your holiday date into an object, with the date being the key
generate today's date
if it's after noon, get tomorrow's date - d.setDate(d.getDate() + 1)
create an empty array
add one day (per #3 above)
check if the new day is Saturday or Sunday, if so, go back to #5
check if the new day is in the holiday list, if so, go back to #5
add the new date to the array
repeat until you have 10 entries
That should give you the next 10 business days in your array. Pick the ones you need to fill out your date picker.