Copied to clipboard

Flag this post as spam?

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


  • Morten 105 posts 345 karma points
    Mar 30, 2017 @ 12:14
    Morten
    0

    Set custom publish date, but publish now if it's empty

    I am trying to make a custom publish date, because the user wants to use that as the publishing date and for sorting. The date will also be displayed on the page.

    Here is what I want:

    1. The user can input a date
    2. The date can be empty (meaning it will be published now)
    3. It has to use that date for sorting
    4. The date has to be set to UTC time, so it's equal for everyone in the world

    I am desperate and I cannot figure out how to do this.

    Here is what I have tried so far: I found a neat little plugin, which displays the user's current UTC time next to the input field, so the person knows their UTC time. I modified that to always enter the current date in the input field:

    var timer = setInterval(function () {
        var date = $(".custom-date").val();
        if (date === "") {
            $(".custom-date").focus();
            $(".custom-date").click();
            $(".custom-date").trigger("click");
            //the date has now been set on the input field
        } else if (date !== "") {
            var newDate = new Date(date);
            var stringDate = newDate.getFullYear() + "-" + ('0' + (newDate.getMonth() + 1)).slice(-2) + "-" + ('0' + (newDate.getDate() - 1)).slice(-2) + " " + ('0' + (newDate.getHours() - offset)).slice(-2) + ":" + ('0' + newDate.getMinutes()).slice(-2) + ":" + ('0' + newDate.getSeconds()).slice(-2);
            $(".custom-date").val(stringDate);
            angular.element(".custom-date").scope().$apply(function () {
                angular.element(".custom-date").scope().datetimePickerValue = stringDate;
            });
            clearInterval(timer);
        }
    }, 1000);
    

    Yes, this looks like a lot... and no, it does not work. I do the focus/click/trigger on the element, because that will automatically set the time to be the user's local time. I then turn that into UTC time (offset is the UTC time offset). Then I apply the date to the element's scope and the value gets updated both in $scope and in the view (I can actually see it).

    However, when I hit save and publish, the date gets reset (it's empty in the database). It's only when I physically click on the input field and select a new date it will actually update it. I like this method, as I am in 100% control of it, so is it possible? It would seem like setting the new date on the scope doesn't trigger the actual "new date has been selected".

    Alternatively I have my Razor code here:

    //selection is all my elements/nodes
    selection.OrderByDescending(x => x.GetProperty("publishDate") != null).ThenByDescending(x => x.GetPropertyValue("publishDate")).Where(x => x.GetPropertyValue<DateTime>("publishDate") < DateTime.UtcNow);
    

    Please help me out with this. I am desperate and I cannot figure out the best way to do this.

  • Morten 105 posts 345 karma points
    Mar 30, 2017 @ 12:46
    Morten
    0

    So apparently before the Angular event is triggered, I need to, at least, call these:

    $(".custom-date").trigger("click");
    $(".custom-date").change();
    

    So I did that right after I set my new date and now it works.

  • 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.

    Continue discussion

Please Sign in or register to post replies