Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Apr 01, 2014 @ 09:32
    Anthony Candaele
    0

    Datetime picker, only show time when not 00:00:00

    Hi,

    For an activity doctype, I use a datetime picker for the start- and enddate. However, my customer needs the possibility to not show time data for some activities.

    My code for the moment looks like this:

    @if (activity.HasValue("activityStartDate"))
                            {
                                <p>
                                <i class="icon-calendar"></i>&nbsp;&nbsp;
                                @if (activity.HasValue("activityEndDate"))
                                {
    
                                @umbraco.library.FormatDateTime(activity.GetPropertyValue("activityStartDate").ToString(), "dd/MM/yyyy hh:mm tt") @:- @umbraco.library.FormatDateTime(activity.GetPropertyValue("activityEndDate").ToString(), "dd/MM/yyyy hh:mm tt")
                                }
                                else
                                {
                                @umbraco.library.FormatDateTime(activity.GetPropertyValue("activityStartDate").ToString(), "dd/MM/yyyy hh:mm tt")
                                }
                                </p>
                            }
    

    Is there a way not to show time one an activity item, for example when time is set to 00:00:00, or something similar?

    thanks for your help,

    Anthony

  • Fuji Kusaka 2203 posts 4220 karma points
    Apr 01, 2014 @ 10:08
    Fuji Kusaka
    1

    Hi Anthony,

    May be you can get this working 

    List<DynamicNode> activity = @Model.AncestorOrSelf(1).Descendants("ActivityDocType").Items;

    foreach(var a in activity){
    if(a.GetProperty("
    activityStartDate").Value.ToString() != " 00:00:00"){
    @Convert.ToDateTime(a.GetProperty("activityStartDate").Value).Date.ToString("dd MMMM, yyy")
    }
  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Apr 01, 2014 @ 10:37
    Jeavon Leopold
    0

    Hi Anthony,

    I use C# Date comparison and it would be something like this (I'm assuming you are using MVC or a Macro Partial View here

    @if (activity.HasValue("activityStartDate"))
    {
        <p>
        <i class="icon-calendar"></i>
            @(activity.GetPropertyValue<DateTime>("activityStartDate").ToString("dd/MM/yyyy hh:mm tt"))
    
            @if (activity.HasValue("activityEndDate"))
            {
                var activityEndDate = activity.GetPropertyValue<DateTime>("activityEndDate");
    
                if (activityEndDate.TimeOfDay == new TimeSpan(0))
                {
                    <text> - @(activity.GetPropertyValue<DateTime>("activityEndDate").ToString("dd/MM/yyyy"))</text>
                }
                else
                {
                    <text> - @(activity.GetPropertyValue<DateTime>("activityEndDate").ToString("dd/MM/yyyy hh:mm tt"))</text>
                }                  
            }   
        </p>
    }
    

    Jeavon

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Apr 01, 2014 @ 10:43
    Jeavon Leopold
    100

    Perhaps slightly more concise would be:

    @if (activity.HasValue("activityStartDate"))
    {
        <p>
        <i class="icon-calendar"></i>
            @(activity.GetPropertyValue<DateTime>("activityStartDate").ToString("dd/MM/yyyy hh:mm tt"))
    
            @if (activity.HasValue("activityEndDate"))
            {
                var activityEndDate = activity.GetPropertyValue<DateTime>("activityEndDate");
    
                var dateMask = "dd/MM/yyyy hh:mm tt";
    
                if (activityEndDate.TimeOfDay == new TimeSpan(0))
                {
                    dateMask = "dd/MM/yyyy";                               
                }            
                <text> - @(activity.GetPropertyValue<DateTime>("activityEndDate").ToString(dateMask))</text>
            }   
        </p>
    }
    
  • Anthony Candaele 1197 posts 2049 karma points
    Apr 01, 2014 @ 11:23
    Anthony Candaele
    0

    Hi Fuji,

    I tested out your solution, and I noticed that a Date object has a TimeofDay property, which is set to "00:00:00" when no time is chosen with the Datetime picker.

    So I came up with this solution which works fine:

    DateTime startDate = (DateTime)activity.GetPropertyValue("activityStartDate");
    @if (startDate.TimeOfDay.ToString() != "00:00:00")
     {
       @umbraco.library.FormatDateTime(activity.GetPropertyValue("activityStartDate").ToString(), "dd/MM/yyyy hh:mm tt")
     }
    else {
           @umbraco.library.FormatDateTime(activity.GetPropertyValue("activityStartDate").ToString(), "dd/MM/yyyy")
     }
    
  • Anthony Candaele 1197 posts 2049 karma points
    Apr 01, 2014 @ 11:27
    Anthony Candaele
    0

    @Jeavon, sorry, my latest post crossed with your solutions. We both used the TimeofDay property in our solutions, but as your where first I'm checking your solution as solved.

    thanks,

    Anthony

  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Apr 01, 2014 @ 11:40
    Jeavon Leopold
    1

    Awesome, I would probably perfer to compare DateTime objects rather than strings but they should give the same result.

    e.g. @if (startDate.TimeOfDay.ToString() != "00:00:00") vs if (startDate.TimeOfDay == new TimeSpan(0))

  • Fuji Kusaka 2203 posts 4220 karma points
    Apr 01, 2014 @ 11:44
    Fuji Kusaka
    1

    @anthony no worries , i myself didnt try it before but thanks @jeavon. 

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 01, 2014 @ 13:38
    Anthony Candaele
    1

    @Jeavon, thanks Jeavon, I refactored my code base on your example. I also added a check to see if start- and enddate are on the same day. If they are, the date is only shown once, like this:

    14/03/2014 09:00 AM - 04:00 PM
    

    My code looks like this now:

         @if (activity.HasValue("activityStartDate"))
                            {
                                var startDate = activity.GetPropertyValue<DateTime>("activityStartDate");
                                var startDateMask = "dd/MM/yyyy hh:mm tt";
                                <p>
                                <i class="icon-calendar"></i>&nbsp;&nbsp;
    
                                @if (startDate.TimeOfDay == new TimeSpan(0))
                                {
                                    startDateMask = "dd/MM/yyyy";
                                }
    
    
                                <text>@(activity.GetPropertyValue<DateTime>("activityStartDate").ToString(startDateMask))</text>
    
                                @if (activity.HasValue("activityEndDate"))
                                {
                                   var endDate = activity.GetPropertyValue<DateTime>("activityEndDate");
                                   var endDateMask = "dd/MM/yyyy hh:mm tt";
                                   if (endDate.Day == startDate.Day)
                                   {
                                       endDateMask = "hh:mm tt";
                                   }
                                   if (endDate.TimeOfDay == new TimeSpan(0))
                                   {
                                       endDateMask = "dd/MM/yyyy";
                                   }
    
    
                                       <text>- @(activity.GetPropertyValue<DateTime>("activityEndDate").ToString(endDateMask))</text>
    
    
                                }
    
                                </p>
                            }
    
  • Jeavon Leopold 3008 posts 13221 karma points MVP 7x admin c-trib
    Apr 01, 2014 @ 13:42
    Jeavon Leopold
    1

    That looks great to me!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies