0

I am working on a sharepoint 2013 on-premises team site. now i want to set a column which is of type date/time to be equal to today date,so i added a script inside the Edit form, then i tried to set the date value using SPUtiltiy, as follow:-

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd = '0'+dd
} 

if(mm<10) {
    mm = '0'+mm
} 

today = dd + '/' + mm + '/' + yyyy;
alert(today);
SPUtility.GetSPFieldByInternalName('OrderDateCustomerApproved_').SetValue(today);

but i got this error:-

Unable to set date, invalid arguments (requires year, month, and day as integers).

throw "Unable to set date, invalid arguments (requires year, month, and day as integers).";

so i tried to do so using pure JavaScript appraoch as follow:-

$('select[id^="OrderDateCustomerApproved_"]').val(today);

where this did not raise any error, but the field was not populated with today date!!

so can anyone adivce on this please?

enter image description here

1

My data column is: enter image description here

A demo code based on my data column for your reference:

<script src="http://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var today = new Date();   
    var dd = today.getDate();   
    var mm = today.getMonth() + 1;   
    var yyyy = today.getYear();   
    if(dd<10) {
    dd = '0'+dd
    } 

    if(mm<10) {
    mm = '0'+mm
    } 

    today = dd + '/' + mm + '/' + yyyy; 
    $("input[id^='Date_x0020_Customer_x0020_Approv_']").val(today);
})
</script>

Note: you need to change id in the above code to yours.

1

I use the following piece of code to set value to Date-time field with SPUtility and jQuery

var currentDate = new Date();
$(SPUtility.GetSPFieldByInternalName(internalFieldName).Controls).find('input').first().val((currentDate.getMonth() + 1) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear());

My Site collection uses MM/DD/YYYY format for dates so i'm setting it in that format. You will have to change the format if needed.

0

I was working on something similar last week. Maybe this will point you in the right direction.

<script>
'use strict';
(function() {

    var timeSinceFieldViewCtx = {};

    timeSinceFieldViewCtx.Templates = {};
    timeSinceFieldViewCtx.Templates.Fields = {

        "Time": {  /* Change to your Date Column - Same as below */ 
            "View": timeSinceFieldViewTemplate
        }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(timeSinceFieldViewCtx);
})();

function timeSinceFieldViewTemplate(ctx) {
    var dateDiff = new Date() - new Date(ctx.CurrentItem.Time); /* Change to your Date Column */ 
    var daysDiff = Math.floor(dateDiff / 1000 / 60 / 60 / 24);
    return daysDiff;
}
window.addEventListener('DOMContentLoaded', function() {

    var x = document.getElementsByClassName('ms-vb-lastCell');

    for( var i =0; i < x.length; ++i ) {

        console.log(x[i].innerText + " Type Of: " + typeof Number(x[i].innerText));

        if (Number(x[i].innerText) > 30 ) { 
            x[i].style.color = 'green';
            x[i].style.fontWeight='normal';
        }
        if (Number(x[i].innerText) > 60 ) { 
            x[i].style.color = 'green';
            x[i].style.fontWeight= 'bold';
        }
    }

}, true);
</script>
  • thanks for the code will try it now,, but why my code which looks shorter compared ur code did not work?? – john Gu Apr 19 '18 at 0:30
  • You're trying to achieve something different from what i did. I think your code only applies to the "New" or "Edit" Form. Do you want to the set the date on a new form or in the list itself? – Jaitsujin Apr 19 '18 at 23:46
0

SharePoint adds a format function to a date object, so you can use that to do your formatting as well. And I don't used the ID of the field, I use the title because that makes things easier to read.

$("input[title='Date Customer Approved']").val(today.format('MM/dd/yyyy'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.