Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get week number, month & year from textfield date 1

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
H,

my js knowledge is pretty basic however, I have a page where the user will enter a date into a textfield, once the date has been entered I would like to get the

week no
month
year

from that date

initially I have tried to write a function to get the month

Code:
<script language="javascript">
function gDate(){
var mydate = document.getElementById("txtDate").value;
month = mydate.getMonth();
document.getElementById("txtMonth").value=(month);
}
</script>

however this does not work could someone possibly have a look for me and see what I am doing wrong

Regards

Paul

 
Hi

The [tt]String[/tt] object has no getMonth() method. Only [tt]Date[/tt] has :
Code:
[b]function[/b] [COLOR=darkgoldenrod]gDate[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] mydate [teal]=[/teal] [b]new[/b] [highlight][COLOR=darkgoldenrod]Date[/color][teal]([/teal][/highlight]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"txtDate"[/i][/green][teal]).[/teal]value[highlight][teal])[/teal][/highlight][teal];[/teal]
  month [teal]=[/teal] mydate[teal].[/teal][COLOR=darkgoldenrod]getMonth[/color][teal]()[/teal][highlight][teal]+[/teal][purple]1[/purple][/highlight][teal];[/teal] [gray]// you may want to make it "human-readable"[/gray]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"txtMonth"[/i][/green][teal]).[/teal]value[teal]=[/teal]month[teal];[/teal]
[teal]}[/teal]


Feherke.
 
Hi feherke,

thanks for replying, I have tried out the code and it seems to work although it is diplaying the wrong value.

this is because the date is entered in as a UK date i.e.

dd/mm/yyyy

but the function is obviously expecting

mm/dd/yyyy

is there any way to solve this please

Regards

Paul

 
Hi Feherke,

thanks for the link I have looked at the code however

Code:
function function1(Sales_Stage){  
var user_date = document.form[0]._course_start_date.value;  var datepart = user_date.split('/');
var set_user_date = new Date(parseInt(datepart[2],10),parseInt(datepart[1],10)-1,parseInt(datepart[0],10));
var Sales_Stage_value = Sales_Stage.options[Sales_Stage.selectedIndex].text;
set_user_date.setDate(set_user_date.getDate()+dayspan[Sales_Stage_value]);
alert(set_user_date.getDate()+"/"+(set_user_date.getMonth()+1)+"/"+set_user_date.getFullYear());
}


to be honest I am not sure how to incorporate this into my present code

Regards

Paul

 
Hi

Code:
[b]function[/b] [COLOR=darkgoldenrod]gDate[/color][teal]()[/teal]
[teal]{[/teal]
  [highlight][b]var[/b] part [teal]=[/teal][/highlight] document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"txtDate"[/i][/green][teal]).[/teal]value[highlight][teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]'/'[/i][/green][teal])[/teal][/highlight][teal];[/teal]
  [b]var[/b] mydate [teal]=[/teal] [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][highlight][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]2[/purple][teal]],[/teal][purple]10[/purple][teal]),[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]1[/purple][teal]],[/teal][purple]10[/purple][teal])-[/teal][purple]1[/purple][teal],[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]0[/purple][teal]],[/teal][purple]10[/purple][teal])[/teal][/highlight][teal]);[/teal]
  month [teal]=[/teal] mydate[teal].[/teal][COLOR=darkgoldenrod]getMonth[/color][teal]()+[/teal][purple]1[/purple][teal];[/teal] [gray]// you may want to make it "human-readable"[/gray]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"txtMonth"[/i][/green][teal]).[/teal]value[teal]=[/teal]month[teal];[/teal]
[teal]}[/teal]

Feherke.
 
Hi Feherke,

Thank you so much, I have now used this to get both the month and the year. All i have to do now is figure out how to get the week no

A Star is on it's way both for your help and assistance

Regards

Paul

 
Hi feherke,

I had a look for some scripts and found this one however I think there may be a syntax eror in it as line 3 does not right with the syntax colouring.

would you mind taking a look please

Code:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}

Regards

Paul

 
Hi

[tt]source-highlight[/tt]'s syntax coloring says it is correct :
Code:
Date[teal].[/teal][b]prototype[/b][teal].[/teal]getWeek [teal]=[/teal] [b]function[/b][teal]()[/teal] [teal]{[/teal]
[b]var[/b] onejan [teal]=[/teal] [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][b]this[/b][teal].[/teal][COLOR=darkgoldenrod]getFullYear[/color][teal](),[/teal][purple]0[/purple][teal],[/teal][purple]1[/purple][teal]);[/teal]
[b]return[/b] Math[teal].[/teal][COLOR=darkgoldenrod]ceil[/color][teal](((([/teal][b]this[/b] [teal]-[/teal] onejan[teal])[/teal] [teal]/[/teal] [purple]86400000[/purple][teal])[/teal] [teal]+[/teal] onejan[teal].[/teal][COLOR=darkgoldenrod]getDay[/color][teal]()+[/teal][purple]1[/purple][teal])/[/teal][purple]7[/purple][teal]);[/teal]
[teal]}[/teal]
Trying it also says it is correct. Meaning functional.

However there are some rules, saying if January 01 is after a certain day of the week, that week belongs to the previous year. I see no such rule reproduced there. So I would not stop at Google's first result.

Feherke.
 
OK Thank you,

I have been looking elsewhere as well

Regards

Paul

 
Hi Feherke,

I have found this code for week number which seems to work however I am not sure how I would go about using it to give me my week number from my textfield date

Code:
<script type="text/javascript" language="JavaScript"><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function getWeek(year,month,day) {
    var when = new Date(year,month,day);
    var newYear = new Date(year,0,1);
    var modDay = newYear.getDay();
    if (modDay == 0) modDay=6; else modDay--;

    var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) -
                 Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;

    if (modDay < 4 ) {
        var weeknum = Math.floor((daynum+modDay-1)/7)+1;
    }
    else {
        var weeknum = Math.floor((daynum+modDay-1)/7);
        if (weeknum == 0) {
            year--;
            var prevNewYear = new Date(year,0,1);
            var prevmodDay = prevNewYear.getDay();
            if (prevmodDay == 0) prevmodDay = 6; else prevmodDay--;
            if (prevmodDay < 4) weeknum = 53; else weeknum = 52;
        }
    }

    return + weeknum;
}

var now = new Date();
document.write('Week number = ' +
getWeek(y2k(now.getYear()),now.getMonth(),now.getDate()));
//--></script>

Regards

Paul

 
Hi

You have to call getWeek() passing the year, month and day as parameter. The same 3 values you passed to [tt]Date[/tt]'s constructor.
Code:
[b]function[/b] [COLOR=darkgoldenrod]gDate[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] part [teal]=[/teal] document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"txtDate"[/i][/green][teal]).[/teal]value[teal].[/teal][COLOR=darkgoldenrod]split[/color][teal]([/teal][green][i]'/'[/i][/green][teal]);[/teal]
  [b]var[/b] mydate [teal]=[/teal] [b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]2[/purple][teal]],[/teal][purple]10[/purple][teal]),[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]1[/purple][teal]],[/teal][purple]10[/purple][teal])-[/teal][purple]1[/purple][teal],[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]0[/purple][teal]],[/teal][purple]10[/purple][teal]));[/teal]
  month [teal]=[/teal] mydate[teal].[/teal][COLOR=darkgoldenrod]getMonth[/color][teal]()+[/teal][purple]1[/purple][teal];[/teal]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"txtMonth"[/i][/green][teal]).[/teal]value[teal]=[/teal]month[teal];[/teal]
  [highlight]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"txtWeek"[/i][/green][teal]).[/teal]value[teal]=[/teal][COLOR=darkgoldenrod]getWeek[/color][teal]([/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]2[/purple][teal]],[/teal][purple]10[/purple][teal]),[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]1[/purple][teal]],[/teal][purple]10[/purple][teal])-[/teal][purple]1[/purple][teal],[/teal][COLOR=darkgoldenrod]parseInt[/color][teal]([/teal]part[teal][[/teal][purple]0[/purple][teal]],[/teal][purple]10[/purple][teal]));[/teal][/highlight]
[teal]}[/teal]

Feherke.
 
Hi Feherke,

I cannot thank you enough for your help with this,

I have added all of the pieces you gave me together now and I can get the week, month and year in one go. for your info this is what it looks like

Code:
function gDate(){  
var part = document.getElementById("txtDate").value.split('/'); 
var mydate = new Date(parseInt(part[2],10),parseInt(part[1],10)-1,parseInt(part[0],10));  
month = mydate.getMonth()+1;
year = mydate.getYear(); // you may want to make it "human-readable"
document.getElementById("txtMonth").value=month;
document.getElementById("txtYear").value=year;
document.getElementById("txtWeek").value=getWeek(parseInt(part[2],10),parseInt(part[1],10)-1,parseInt(part[0],10));
}

Once again thank you so much for your help

[2thumbsup][bigsmile][2thumbsup]


Regards

Paul

 
Hi

Paul said:
Code:
year = mydate.getYear(); // you may want to make it "human-readable"
Well, getting the year as 109 instead of 2009 is not human-readable for me. Because that is what the [tt]getYear()[/tt] method returns according to the standard. ( Of course, Explorer is different, as usually. ) Beside that, [tt]getYear()[/tt] is deprecated since JavaScript 1.3. Use [tt]getFullYear()[/tt] instead.

Feherke.
 
Hi Feherke,

Blimey, so much to learn, thank goodness there are people like you around to help numpties like me out.

I did only check this in IE as this will be used on our company Intranet and that is all people have installed however,

after your post I did try it in Firefox and I see what you mean.

I have changed it to getFullYear as you suggested.

Thanks again for your help [2thumbsup]:-D[2thumbsup]

Regards

Paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top