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

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:
The user can input a date
The date can be empty (meaning it will be published now)
It has to use that date for sorting
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);

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.

Related

How to get yesterday date in Delphi

I know you can get the current date and time using the Now() function, but how would you get the date of yesterday?
You can use the Yesterday or IncDay(Now,-1) function from System.DateUtils as follow:
uses System.DateUtils;
begin
// Example 1:
ShowMessage('Yesterday = ' + DateToStr(Yesterday)); // Date of yesterday
ShowMessage('Today = ' + DateToStr(Date)); // Date of Today
ShowMessage('Tomorrow = ' + DateToStr(tomorrow)); // Date of tomorrow
// Example 2:
ShowMessage('Yesterday = ' + DateToStr(IncDay(Now,-1))); // Date of yesterday
ShowMessage('Today = ' + DateToStr(Now)); // Date of Today
ShowMessage('Tomorrow = ' + DateToStr(IncDay(Now,1))); // Date of tomorrow
end;
These functions return a TDateTime data type. The time component is set to zero.
Why not just use Date - 1 ?
Since one day is 1.0 in TDateTime encoding, substracting 1 is enough.

How to write a script which concatenates namedValues?

I have a google sheet populated by a google form input with 1 column that has a start date, the 2nd has the start time (the time is a text input from a drop down menu in a time format (but it is text) - 09:00 - 09:30 - 10:00 ..etc) I'm trying to join the date and time input to create a calendar event with the end date the same day as the start day and end time 30 minutes after. Any help would be appreciated!
I have searched and found that the text string for time can be converted by removing the ' sign infront of the text time input by using:-
var withoutQuote = e.values[1].substring(1);
My current script for createEvent:-
var options = { description: namedValues.Description[0],
location: namedValues.Location[0],
guests:"info#domain.com"};
var cEvent = CalendarApp.getCalendarsByName("TEST")[0].createEvent(
namedValues.Name[0],
new Date(namedValues.Starts),
new Date(namedValues.Ends),
options)
}
I would like to create new date based on input date (namedValues.Starts) + concatenate start time (namedValues.Stime) and new date based on (namedValues.Starts) + concatenate start time (namedValues.Stime) + 30 minutes
Any help would be appreciated!
Ok, I was able to get the concatenates namedValues part right herewith edit code:-
function createEvent_ (namedValues) {
var options = { description: namedValues.Description[0],
location: namedValues.Location[0],
guests:"johan#inprint.co.za"};
var cEvent = CalendarApp.getCalendarsByName("TEST")[0].createEvent(
namedValues.Name[0],
new Date(namedValues.Starts + " " + namedValues.Stime),
new Date(namedValues.Ends + " " + namedValues.Etime),
options)
}
What I need now is the second part - I would like to create a new end date/time - based on new Date(namedValues.Starts + " " + namedValues.Stime) + 30 minutes

How can I get the Kendo UI MVC datetimepicker to format the date unambiguously in the URL/form?

I'm trying to use the Kendo UI MVC dateTimePicker to format the date unambiguously so that MVC model binding understands it. I have a datetimepicker control in a form which submits via a GET request.
MVC expects dates in this format: yyyy-MM-dd but I want to display dates on the page as dd/MM/yyyy
If I use this code, MVC parses the date correctly, but displays the year first to the user, which isn't what I want:
#(Html.Kendo().DateTimePicker()
.Name("ToDate")
.Format("yyyy-MM-dd")
)
Is there a workaround so I can format the date for humans in the box, but format it in an unambiguous (invariant) format in the URL?
You should configure your DateTimePicker format to a user friendly format. Then use javascript to get the Date object from the widget (not string date). Then format this date object to 'yyyy-mm-dd' and make the request.
So your widget shoul be:
#(Html.Kendo().DateTimePicker()
.Name("ToDate")
.Format("dd/MM/yyyy")
)
Then use this javascript to make the request:
var date = $("#ToDate").data("kendoDateTimePicker").value();
date variable have a javascript date you can format:
function dateToString(date){
var str = date.getFullYear() + "-";
str += ('0' + (date.getMonth() + 1)).slice(-2) + "-";
str += ('0' + date.getDate()).slice(-2) + "T";
str += ('0' + date.getHours()).slice(-2) + "-";
str += ('0' + date.getMinutes()).slice(-2) + "-";
str += ('0' + date.getSeconds()).slice(-2);
return str;
}
This format is "yyyy-mm-dd hh:mm:ss", but you can remove the time part if you want

Geting DataTime from database through Javascript

I have a MVC Web Application Im trying to Ajax call an action method to retrieve some datetime from the database, the problem is the value comes as "/Date(386028000000)/"
its a DateOfBirth actually which I m using a java script function to calculate the age:
function (DOB) {
var birthday = +new Date(DOB);
return ~~((Date.now() - birthday) / (31557600000));
}
Anyway i can fix the Date Format and get only the date in a proper format or change the Java-Script method to accept the current format of the date value ?
I got it
var FixedDate = new Date();
FixedDate .setTime(DOB.replace("/Date(", "").replace(")/", ""));
return ~~((Date.now() - FixedDate) / (31557600000));
Click here to check the Demo
Sample Javascript/JQuery
var = MyDate_String_Value = "/Date(386028000000)/"
var value = new Date
(
parseInt(MyDate_String_Value.replace(/(^.*\()|([+-].*$)/g, ''))
);
var dat = value.getMonth() +
1 +
"/" +
value.getDate() +
"/" +
value.getFullYear();
Result - "3/27/1982"

JQUERY call to Controller Action: String Parameter truncated if containing 'space' character

I have a view that accepts 2 string parameters and 2 date values. User hits search button and they get filtered output to the screen. This all works perfectly well until a user inputs a string with a space. i.e. they can search for 'waste' but not 'waste oil'.
Interestingly, in the latter, the parameter is ok from Javascript before the call is made. But on entering the controller code it goes form being 'waste oil' on client to 'waste'. When this happens the other parameters get set to NULL crashing the system.
I've tried replacing the spaces if present with '#' character then stripping out and putting back in ' ' on the controller side. This is a messy fudge and only appears to work with one parameter.
There must be a simple explanation for this parameter data loss, any comments much appreciated
Not sure a code example is needed but here it is anyway if it help:
My controller header :
public ActionResult IndexSearch(int? page, string searchText,string searchTextSite,string StartDate,string EndDate)
{
My HTML Javascript :
function Search(sSearchText,sSite) {
sSearchText = sSearchText.toString().replace(" ", "#");
sSite = sSite.toString().replace(" ", "#");
debugger;
alert($("#AbsolutePath").val() + "Waste.mvc/IndexSearch?searchText=" + sSearchText + "&searchTextSite=" + sSite + "&StartDate=" + $('#StartDate').val() + "&EndDate=" + $('#EndDate').val());
$("#ResultsList").load($("#AbsolutePath").val() + "Waste.mvc/IndexSearch?searchText=" + sSearchText + "&searchTextSite=" + sSite + "&StartDate=" + $('#StartDate').val() + "&EndDate=" + $('#EndDate').val(),
function() {
$('#LoadingGif').empty();
});
$('#LoadingGif').empty().html('<img src="' + $("#AbsolutePath").val() + 'Content/images/ajax-loader.gif" alt="Loading image" />');
}
You are not URL encoding your parameters when sending the AJAX request because you are using string concatenations when building the url. You could use the following technique in order to have properly encoded values:
var url = $('#AbsolutePath').val() + 'Waste.mvc/IndexSearch';
var data = {
searchText: sSearchText,
searchTextSite: sSite ,
StartDate: $('#StartDate').val(),
EndDate: $('#EndDate').val()
};
$('#ResultsList').load(url, data, function() {
$('#LoadingGif').empty();
});
Now you will get correct values on the server.

Resources