postgresql date_trunc() function in javascript - javascript

What would be an alternative of postgresql date_trunc() function in javascript?
If I typed in pgsql the following:
select date_trunc('month', now())
I'd get '2015-08-01 00:00:00-07'
How to achieve the same with a javascript function?
function dateTruncate(interval, date) {
return ???;
}

Using MomentJS
$("#result").text(moment().format("YYYY-MM-DD 00:00:00Z"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
<div id="result"></div>

ended up using moment:
function dateTruncate(interval, date) {
return moment(date).startOf(interval).toDate();
}

Related

Limit Number of attachments with Java on a SharePoint new form item?

I'm currently using this bit of code in a content editor on the newform in a sharepoint list to check and make sure there is an attachment on a new item form:
<script type="text/javascript" language="javascript">
function PreSaveAction() { if
(document.getElementById('idAttachmentsRow').style.display=='none' ) { >
alert('Attachment is Mandatory! Please attach Documents.'); return false ; }
else { return true; } } </script>
Is there anyway to use some similar code to make sure a user only uploads 1 attachment and not multiples?
Bonus question: Is there a simple method to either set a column value when a user changes/upload a different/new attachment or replaces the current one atatched to a sharepoint list item?
Try below js solution.
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
function PreSaveAction() {
var attachementCount=$('#idAttachmentsTable').find('tr').length;
if (attachementCount < 1) {
alert("Attachment is Mandatory!");
return false;
} else
return true;
}
</script>

javascript moment.js format date

I am doing in accordance with this question and I need to format my resulting time string. I use bootstrap datetimepicker and moment.js and I have this code:
$("#datetimepicker2").on("dp.change", function (e) {
console.log('date2: ' + e.date.format('MM.DD.YYYY HH:mm:ss') );
end_date = e.date;
end_date.toJSON = function(){ return moment(this).format(); };
});
console.log(JSON.stringify(end_date));
My problem is that I receive
"2017-06-20T17:29:05+03:00"
while I need something like this:
"2017-06-20T17:29:05.013Z"
Any ideas how to get it would be welcome. Thank you.
You can convert the moment object to js date and use toISOString() on that.
moment().toDate().toISOString();
You can add .tz() before .format() and pass in the timezone inside .tz("America/New_York"), etc.
$(function() {
$("#datetimepicker2").datepicker();
$("#datetimepicker2").on("change", function(e) {
console.log(moment().utc($(this).val()).format());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.js"></script>
Date: <input type="text" id="datetimepicker2" />

parse html code from json using native javascript

Is it possible to get out the code that is inside a json file? You can have a look at it here: http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4
What is the code or method should I use to put the html code inside any raw div?
I tried to use jsonp, but there is a mistake:
unexpected token <
<script>
function myFunction(data){
var arr = JSON.parse(data);
document.getElementById('advBlock').innerHTML = arr;
}
</script>
<script type="text/javascript" src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>
The JSONP data is already a Javascript object, not a JSON string, so you don't need to parse it:
<div id="advBlock"></div>
<script>
function myFunction(data) {
document.getElementById('advBlock').innerHTML = data;
}
</script>
<script src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>
<div id="advBlock"></div>
<script src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>
<script> function myFunction(data) { document.getElementById('advBlock').innerHTML = data; } </script>
You have to add the script before your function call.

How to check for invalid argument in JS?

What can I do to attain the below thing... The argument generated is bit weird!
function function1(argument1,argument2)
{
if argument1 = "
") do something;
}
"argument1" and "argument2" are generated by the CMS. I can't do anything with those contents.
Either it will generate:
<script type='text/javascript'>
document.write(function1("argument1","argument2"));
</script>
OR
<script type='text/javascript'>
document.write(function1("
","argument2"));
</script>
You can use String.prototype.trim():
function function1(argument1,argument2)
{
if(argument1.trim() == ''){
// do something
}
}
If you're worried about old browsers, you can implement the trim function yourself, see this question.

How to access javascript value in a4j

I need to set a Bean value with one javascript return value.
Something like:
<script type="text/javascript">
function getUserId(){
return 4;
}
</script>
<h:inputText name="lala" value="getUserId()"/>
Thanks
I solved it.
I was using a:jsFunction tag as it follows:
<script type="text/javascript">
function getUserId(){
var user = MyCompany.get_User();
return user;
}
</script>
<a:jsFunction action="#{user.performLogin()}" name="doSiteLogin" >
<a:actionparam name="uid" value="getUserId()"/>
</a:jsFunction>
If you use the property noEscape="true" on the a:actionparam ... it call your javascript code.

Categories