Skip to content

Commit

Permalink
+ Fix the age and anniversary on person profile page, and the birthda…
Browse files Browse the repository at this point in the history
…y picker on person edit page to be culture aware when formatting date display ( Fixes #607 ).
  • Loading branch information
azturner committed Oct 29, 2014
1 parent ad35803 commit e999193
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
8 changes: 8 additions & 0 deletions Rock/Utility/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,14 @@ public static long ToJavascriptMilliseconds( this DateTime dateTime )
return (long)( dateTime.ToUniversalTime() - new DateTime( 1970, 1, 1 ) ).TotalMilliseconds;
}

public static string ToMonthDayString( this DateTime dateTime )
{
var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
string mdp = dtf.ShortDatePattern;
mdp = mdp.Replace( dtf.DateSeparator + "yyyy", "" ).Replace( "yyyy" + dtf.DateSeparator, "" );
return dateTime.ToString( mdp );
}

#endregion

#region TimeSpan Extensions
Expand Down
43 changes: 36 additions & 7 deletions Rock/Web/UI/Controls/Pickers/BirthdayPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// </copyright>
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

Expand Down Expand Up @@ -279,14 +281,41 @@ public void RenderBaseControl( HtmlTextWriter writer )
dayDropDownList.AutoPostBack = needsAutoPostBack;
yearDropDownList.AutoPostBack = needsAutoPostBack;

writer.AddAttribute("class", "form-control-group");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute( "class", "form-control-group" );
writer.RenderBeginTag( HtmlTextWriterTag.Div );

// Get date format and separater for current culture
var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
string mdp = dtf.ShortDatePattern;
var dateParts = dtf.ShortDatePattern.Split( dtf.DateSeparator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries ).ToList();
if ( dateParts.Count() != 3 ||
!dateParts.Contains( "MM" ) ||
!dateParts.Contains( "dd" ) ||
!dateParts.Contains( "yyyy" ) )
{
dateParts = new List<string> { "MM", "dd", "yyyy" };
}
string separatorHtml = string.Format( " <span class='separator'>{0}</span> ", dtf.DateSeparator );

monthDropDownList.RenderControl( writer );
writer.Write(" <span class='separator'>/</span> ");
dayDropDownList.RenderControl( writer );
writer.Write( " <span class='separator'>/</span> " );
yearDropDownList.RenderControl( writer );
for ( int i = 0; i < 3; i++ )
{
switch( dateParts[i])
{
case "MM":
monthDropDownList.RenderControl( writer );
break;
case "dd":
dayDropDownList.RenderControl( writer );
break;
case "yyyy":
yearDropDownList.RenderControl( writer );
break;
}
if ( i < 2)
{
writer.Write( separatorHtml );
}
}

writer.RenderEndTag();
}
Expand Down
4 changes: 2 additions & 2 deletions RockWeb/Blocks/Crm/PersonDetail/Bio.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected override void OnLoad(EventArgs e)
{
string ageText = ( Person.BirthYear.HasValue && Person.BirthYear != DateTime.MinValue.Year ) ?
string.Format( "{0} yrs old ", Person.BirthDate.Value.Age() ) : string.Empty;
lAge.Text = string.Format( "{0}<small>({1})</small><br/>", ageText, Person.BirthDate.Value.ToString( "MM/dd" ) );
lAge.Text = string.Format( "{0}<small>({1})</small><br/>", ageText, Person.BirthDate.Value.ToMonthDayString() );
}

lGender.Text = Person.Gender.ToString();
Expand All @@ -189,7 +189,7 @@ protected override void OnLoad(EventArgs e)

lMaritalStatus.Text = Person.MaritalStatusValueId.DefinedValue();
if ( Person.AnniversaryDate.HasValue )
lAnniversary.Text = string.Format( "{0} yrs <small>({1})</small>", Person.AnniversaryDate.Value.Age(), Person.AnniversaryDate.Value.ToString( "MM/dd" ) );
lAnniversary.Text = string.Format( "{0} yrs <small>({1})</small>", Person.AnniversaryDate.Value.Age(), Person.AnniversaryDate.Value.ToMonthDayString() );

if ( Person.PhoneNumbers != null )
{
Expand Down
6 changes: 5 additions & 1 deletion RockWeb/Blocks/Security/AccountDetail.ascx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ protected override void OnInit( EventArgs e )

if ( CurrentPerson.BirthDate.HasValue )
{
var dtf = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
string mdp = dtf.ShortDatePattern;
mdp = mdp.Replace( dtf.DateSeparator + "yyyy", "" ).Replace( "yyyy" + dtf.DateSeparator, "" );

string ageText = ( CurrentPerson.BirthYear.HasValue && CurrentPerson.BirthYear != DateTime.MinValue.Year ) ?
string.Format( "{0} yrs old ", CurrentPerson.BirthDate.Value.Age() ) : string.Empty;
lAge.Text = string.Format( "{0}<small>({1})</small><br/>", ageText, CurrentPerson.BirthDate.Value.ToString( "MM/dd" ) );
lAge.Text = string.Format( "{0}<small>({1})</small><br/>", ageText, CurrentPerson.BirthDate.Value.ToMonthDayString() );
}

lGender.Text = CurrentPerson.Gender != Gender.Unknown ? CurrentPerson.Gender.ToString() : string.Empty;
Expand Down

0 comments on commit e999193

Please sign in to comment.