c#和java脚本功能 [英] c# and java script funtion
本文介绍了c#和java脚本功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
如何将这个c#函数转换为java脚本函数...请回复
public string GetAge(DateTime birthday)
{
DateTime Now = DateTime。现在;
// 获取年份
int 年= new DateTime(DateTime.Now.Subtract(birthday).Ticks)。年 - 1 < /跨度>;
// 获取过去一年来计算月,日和时间
DateTime dtPastYearDate = birthday.AddYears(年);
int Months = 0 ;
for ( int i = 1 ; i < = 12 ; i ++)
{
if (dtPastYearDate.AddMonths(i)== Now)
{
Months = i;
break ;
}
else if (dtPastYearDate.AddMonths(i)> =现在)
{
Months = i - 1 ;
break ;
}
}
int Days = Now.Subtract(dtPastYearDate.AddMonths(Months))。Days;
int Hours = Now.Subtract(dtPastYearDate).Hours;
int 分钟= Now.Subtract(dtPastYearDate).Minutes;
int Seconds = Now.Subtract(dtPastYearDate).Seconds;
if (年= = 0 && Months == 0 && Days < 4 )
{
int totalhour =(Days * 24 )+ Hours;
return String .Format( 年龄:{0}小时{1}分钟,
totalhour,Minutes);
}
else if (Days > = 4 && Days < = 28 && Years == 0 &&& Months == 0 )
{
return String .Format( 年龄:{0}日,
天);
}
其他 如果(年= = 0 && Months < 3 &&(Days > = 29 ||月份> = 1 )&& Years < = 2 )
{
int weeks =((Months * 30 )+天)/ 7 ;
int remainingday =((Months * 30 )+ Days)% 7 跨度>;
if (remainingday!= 0 )
{
return String .Format( 年龄:{0}周(1)天(,周,剩余日期);
}
其他
{
返回 String .Format( 年龄:{0}周,周);
}
}
其他 如果((年= = 0 && Months > = 3 )||(年= = 1 ))&& Years < 2 )
{
if (年== 1 )
{
Months = Months + 12 ;
}
如果(Days!= 0 )
{
return String .Format( 年龄:{0}个月{1}日,月,日);
}
其他
{
返回 String .Format( 年龄:{0}个月,几个月);
}
}
else
{
if (Days!= 0 )
{
return 字符串 .Format( 年龄:{0}年({ 1}个月{2}天,
年,月,日);
}
其他
{
返回 String .Format( 年龄:{0}年{1}个月,
年,月;
}
}
}
谢谢&问候
Srishti
解决方案
我可以看到你要找的是你的年龄计算,尝试以下javascript函数它将执行相同的function getAge(dateString){
var today = new Date ();
var birthDate = new 日期(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m< 0 ||(m === 0 && today.getDate()< birthDate.getDate())){
age--;
}
返回年龄;
}
alert(getAge(' 10/01/1985'跨度>));
Hello
How to convert this c# function to java script function...Please reply
public string GetAge(DateTime birthday)
{
DateTime Now = DateTime.Now;
//Get Years
int Years = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year - 1;
//Get Past Year to calculate months, days and time
DateTime dtPastYearDate = birthday.AddYears(Years);
int Months = 0;
for (int i = 1; i <= 12; i++)
{
if (dtPastYearDate.AddMonths(i) == Now)
{
Months = i;
break;
}
else if (dtPastYearDate.AddMonths(i) >= Now)
{
Months = i - 1;
break;
}
}
int Days = Now.Subtract(dtPastYearDate.AddMonths(Months)).Days;
int Hours = Now.Subtract(dtPastYearDate).Hours;
int Minutes = Now.Subtract(dtPastYearDate).Minutes;
int Seconds = Now.Subtract(dtPastYearDate).Seconds;
if (Years == 0 && Months == 0 && Days < 4)
{
int totalhour = (Days * 24) + Hours;
return String.Format("Age: {0} Hour(s) {1} Minutes(s)",
totalhour, Minutes);
}
else if (Days >= 4 && Days <= 28 && Years == 0 && Months == 0)
{
return String.Format("Age: {0} Day(s)",
Days);
}
else if (Years == 0 && Months < 3 && (Days >= 29 || Months >= 1) && Years <= 2)
{
int weeks = ((Months * 30) + Days) / 7;
int remainingday = ((Months * 30) + Days) % 7;
if (remainingday != 0)
{
return String.Format("Age: {0} Week(s) {1} Day(s)", weeks, remainingday);
}
else
{
return String.Format("Age: {0} Week(s)", weeks);
}
}
else if (((Years == 0 && Months >= 3) || (Years == 1)) && Years < 2)
{
if (Years == 1)
{
Months = Months + 12;
}
if (Days != 0)
{
return String.Format("Age: {0} Month(s) {1} Day(s)", Months, Days);
}
else
{
return String.Format("Age: {0} Month(s)", Months);
}
}
else
{
if (Days != 0)
{
return String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s)",
Years, Months, Days);
}
else
{
return String.Format("Age: {0} Year(s) {1} Month(s)",
Years, Months);
}
}
}
Thanks & Regards
Srishti
解决方案
Hi,
I can see what you are looking for is to get your age calculated,try the following javascript function it will do the samefunction getAge(dateString) { var today = new Date(); var birthDate = new Date(dateString); var age = today.getFullYear() - birthDate.getFullYear(); var m = today.getMonth() - birthDate.getMonth(); if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { age--; } return age; } alert(getAge('10/01/1985'));
这篇关于c#和java脚本功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文