• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Need to conver the following code from javascript to C@

<SCRIPT LANGUAGE="JavaScript"><!--
function makeArray()    {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var monthsofyear  = new makeArray('January','February','March','April','May','June','July','August','September','October','November','December');

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function ValidDate(day,month,year) {
    if ( (LeapYear(year) && (day > daysofmonthLY[month])) ||
         (!LeapYear(year) && (day > daysofmonth[month])) )
        return false;
    else
        return true;
}

function ChosenDate(object) {
    year = object.year.value;
    month = object.month.options[object.month.selectedIndex].value;
    day = object.day.options[object.day.selectedIndex].value;

    if (!ValidDate(day-0,month-0,year-0)) {
        alert('You must enter a valid date');
        return false;
    }

    if (parent.location.href != window.location.href) {
        parent.year = year;
        parent.month = month;
        parent.day = day;
        parent.outputFrame.location.href = 'output.htm';
    }
    else {
        return true;
    }

    return false;
}
-------------------------------------------------
<SCRIPT LANGUAGE="JavaScript"><!--
function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

function makeArray() {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
        this[i+1] = makeArray.arguments[i];
}

var daysofweek   = new makeArray('Sunday',
                                 'Monday',
                                 'Tuesday',
                                 'Wednesday',
                                 'Thursday',
                                 'Friday',
                                 'Saturday');

var monthsofyear = new makeArray('January',
                                 'February',
                                 'March',
                                 'April',
                                 'May',
                                 'June',
                                 'July',
                                 'August',
                                 'September',
                                 'October',
                                 'November',
                                 'December');

var chinese      = new makeArray('Rat',
                                 'Ox',
                                 'Tiger',
                                 'Hare',
                                 'Dragon',
                                 'Snake',
                                 'Horse',
                                 'Sheep',
                                 'Monkey',
                                 'Fowl',
                                 'Dog',
                                 'Pig');

var branches     = new makeArray('Zi',
                                 'Chou',
                                 'Yin',
                                 'Mao',
                                 'Chen',
                                 'Si',
                                 'Wu',
                                 'Wei',
                                 'Shen',
                                 'You',
                                 'Xu',
                                 'Hai');

var stems        = new makeArray('Jia',
                                 'Yi',
                                 'Bing',
                                 'Ding',
                                 'Wu',
                                 'Ji',
                                 'Geng',
                                 'Xin',
                                 'Ren',
                                 'Gui');

var chinesenewyear = new makeArray(1.31,2.19,2.08,1.29,2.16,2.04,1.25,2.13,2.02,1.22,
                                   2.10,1.30,2.18,2.06,1.26,2.14,2.03,1.23,2.11,2.01,
                                   2.20,2.08,1.28,2.16,2.05,1.25,2.13,2.02,1.23,2.10,
                                   1.30,2.17,2.06,1.26,2.14,2.04,1.24,2.11,1.31,2.19,
                                   2.08,1.27,2.15,2.05,1.25,2.13,2.02,1.22,2.10,1.29,
                                   2.17,2.06,1.27,2.14,2.03,1.24,2.12,1.31,2.18,2.08,
                                   1.28,2.15,2.05,1.25,2.13,2.02,1.21,2.09,1.30,2.17,
                                   2.06,1.27,2.15,2.03,1.23,2.11,1.31,2.18,2.07,1.28,
                                   2.16,2.05,1.25,2.13,2.02,2.20,2.09,1.29,2.17,2.06,
                                   1.27,2.15,2.04,1.23,2.10,1.31,2.19,2.07,1.28,2.16);


function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) + Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d+1;
}

function Nths(day) {
    if (day == 1 || day == 21 || day == 31) return 'st';
    if (day == 2 || day == 22) return 'nd';
    if (day == 3 || day == 23) return 'rd';
    return 'th';
}

function FullDate(day,month,year) {
    return daysofweek[DayOfWeek(day,month,year)] +' '+ day + Nths(day) +' '+ monthsofyear[month] +' '+ year;
}

function Chinese(day,month,year) {
    if ((year > 1900) && (year < 1999)) {
        var newYear = chinesenewyear[year - 1899];
        var newMonth = Math.floor(newYear);
        var newDay = Math.round((newYear*100)-(newMonth*100));

        if ((month > newMonth) || ((month == newMonth) && (day >= newDay))) {
            // this year
            var chineseBranch = (year-3)%12;
            var chineseStem = (year-3)%10;

            var nextYear = chinesenewyear[year - 1899 + 1];
            var nextMonth = Math.floor(nextYear);
            var nextDay = Math.round((nextYear*100)-(nextMonth*100));

            var lastDay = nextDay - 1;
            if (lastDay == 0) {
                lastMonth = nextMonth - 1;
                lastDay = 31;
            }
            else lastMonth = nextMonth;
           
            chineseyearrange = FullDate(newDay,newMonth,year) + ' - ' +
                               FullDate(lastDay,lastMonth,year+1);
        }
        else {
            // last year
            var chineseBranch = (year-4)%12;
            var chineseStem = (year-4)%10;

            var oldYear = chinesenewyear[year - 1899 - 1];
            var oldMonth = Math.floor(oldYear);
            var oldDay = Math.round((oldYear*100)-(oldMonth*100));

            var lastDay = newDay - 1;
            if (lastDay == 0) {
                lastMonth = newMonth - 1;
                lastDay = 31;
            }
            else lastMonth = newMonth;
           
            chineseyearrange = FullDate(oldDay,oldMonth,year-1) + ' ' +
                               FullDate(lastDay,lastMonth,year);
        }
    }
    else {
        var chineseBranch = (year-3)%12;
        var chineseStem = (year-3)%10;
    }

    if (chineseBranch == 0)
        chineseBranch = 12;

    if (chineseStem == 0)
        chineseStem = 10;

    chineseyearname = '(' + stems[chineseStem] + '-' + branches[chineseBranch] + ')';

    return chinese[chineseBranch];
}

if (location.search.length == 0) {
    var year = parent.year - 0;
    var month = parent.month - 0;
    var day = parent.day - 0;
}
else {
    var day   = location.search.substring(5,7) - 0;
    var month = location.search.substring(14,16) - 0;
    var year  = location.search.substring(22) - 0;
}

var today = new Date();
var thisYear = y2k(today.getYear());
var thisMonth = today.getMonth()+1;
var thisDay = today.getDate();
//--></SCRIPT>
</HEAD>

<BODY><CENTER>

<SCRIPT LANGUAGE="JavaScript"><!--
document.write('Todays Date: '+thisDay+'/'+thisMonth+'/'+thisYear+'<BR>');
document.write('Date Entered: '+day+'/'+month+'/'+year+'<BR>');

var chineseyearrange = '';
var chineseyearname = '';
var chineseyear = Chinese(day,month,year);

document.write('Chinese year of the ' + chineseyear + ' ' + chineseyearname + '<BR>' + chineseyearrange + '<BR>');
//--></SCRIPT>

<A HREF="index.htm" TARGET="_parent">Return</A>

</CENTER></BODY>
</HTML>
I think there should be another way of doing this.
function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

function padout(number) { return (number < 10) ? '0' + number : number; }

var today = new Date();
var thisYear = y2k(today.getYear());
var thisMonth = today.getMonth()+1;
var thisDay = today.getDate();

var dayOutput = '';
for (var days=1; days <= 31; days++) {
    if (days == thisDay) dayOutput += '<OPTION VALUE="' + padout(days) + '" SELECTED>' + days;
    else             dayOutput += '<OPTION VALUE="' + padout(days) + '">' + days;
}

var monthOutput = '';
for (var months=1; months <=12; months++) {
    if (months == thisMonth) monthOutput += '<OPTION VALUE="' + padout(months) + '" SELECTED>' + monthsofyear[months];
    else                 monthOutput += '<OPTION VALUE="' + padout(months) + '">' + monthsofyear[months];
}

var yearOutput = '<INPUT TYPE="TEXT" NAME="year" SIZE="4" VALUE="' + thisYear + '">';
//--></SCRIPT>
0
mathieu_cupryk
Asked:
mathieu_cupryk
  • 2
  • 2
  • 2
1 Solution
 
Anurag ThakurCommented:
i will ask a very short question
what is this JavaScript code actually doing
if we get to know that we can definitely help you in getting your logic converted to c#
0
 
mathieu_cuprykAuthor Commented:
it is calculating your chinese sign. This javascript is garbage code.
0
 
Khalid Mehmood AwanCommented:
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mathieu_cuprykAuthor Commented:
I will rewrite the thing in C#
0
 
Anurag ThakurCommented:
0
 
Khalid Mehmood AwanCommented:
Thanks for the points ;-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now