Only want javascript to execute on a specific Rails view - javascript

I've got some coffee script in /assets/javascripts/transactions.coffee that populates a text field (in /views/transactions/_form.html.erb) with today's date in dd/mm/yy format.
$ ->
today = new Date
dd = today.getDate()
mm = today.getMonth() + 1
#January is 0!
yyyy = today.getFullYear()
if dd < 10
dd = '0' + dd
if mm < 10
mm = '0' + mm
today = dd + '/' + mm + '/' + yyyy
$('#transaction_date').val today
$ ->
$('#transaction_date').datepicker({dateFormat: "dd/mm/yy"})
Currently, that form is referenced in /views/transactions/new.html.erb and /views/transactions/edit.html.erb. However, I only want the text field to be populated on new.html.erb and not edit.html.erb.
What's the 'cleanest' way to achieve this in Rails (4.2.5)?

Maybe you can create functions in transactions.coffee that you call from your view with an inline script. For example, in transactions.coffee
this.App || (this.App = {})
this.App.Transactions =
runForNewView: () ->
today = new Date
dd = today.getDate()
mm = today.getMonth() + 1
#January is 0!
yyyy = today.getFullYear()
if dd < 10
dd = '0' + dd
if mm < 10
mm = '0' + mm
today = dd + '/' + mm + '/' + yyyy
$('#transaction_date').val today
Then for example in the new view template, you would just add
<script text="text/javascript">
$(function() { App.Transactions.runForNewView(); });
</script>

Related

set the date and time to current date and time

I want to limit the user from selecting the current date and current time.
What I have tried is:
<input id="datefield" type="date" />
<input type="time" id="timefield" class="form-control" />
var today = new Date();
var thishr = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
var hr = today.getHours();
var mn = today.getMinutes();
if(dd < 10){
dd = '0' + dd
}
if (mm < 10){
mm = '0' + mm
}
today = yyyy + '-' + mm + '-' + dd;
thishr = hr + '-' + mn;
document.getElementById("datefield").setAttribute("max", today);
document.getElementById("datefield").setAttribute("min", today);
document.getElementById("timefield").setAttribute("max", thishr);
document.getElementById("timefield").setAttribute("min", thishr);
Seems like the JavaScript for the date part is working, but the part for the time is not working as expected. Did I miss some important part?
first:
hr and mn have to include leading zeros, and the separator should be a colon, not a hyphen. like this : "08:05".
second:
put your input into a form element with a submit button. you will still be able to select any time, but when you will try to submit the form, the validation won't let you if the selected time is out of range.

How to change the format of full date

Actually I made a function in which picks up the current date. Now i want to pickup the date after 6 months of my current date
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy ; //Here I am getting current date now i want to get the date exact after 6 months in the same format like this
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here I am getting date after 6 months but it is in different format like complete date, I want date after after 6 months like above format
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var date=today;
var x = 6; //or whatever offset
var CurrentDate = new Date();
//alert(CurrentDate);
CurrentDate.setMonth(CurrentDate.getMonth() + x);
var d = String(CurrentDate.getDate()).padStart(2, '0');
var m = String(CurrentDate.getMonth() + 1).padStart(2, '0');
var yy = CurrentDate.getFullYear();
CurrentDate = m + '/' + d + '/' + yy;
var months=CurrentDate;
Just pass new generated timestamp in new Date() and it will give you new date
Like var today_after_month = new Date(timestamp);
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
var today = mm + '/' + dd + '/' + yyyy ; //Here i m getting current date now i want to get the date exact after 6 months in the same format like this
console.log('today', today);
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here i m getting date after 6 months but it is different format like complete date i want date after after 6 months like above format
var today_after_month = new Date(month);
var dd = String(today_after_month.getDate()).padStart(2, '0');
var mm = String(today_after_month.getMonth() + 1).padStart(2, '0');
var yyyy = today_after_month.getFullYear();
var today = mm + '/' + dd + '/' + yyyy ;
console.log('month', today);

How to show current date on input field date [duplicate]

This question already has answers here:
How to set input type date's default value to today?
(43 answers)
Closed 4 years ago.
I want to show current date on input field date.My code is down below
HTML:
<div class=\"col-lg-4\">
<label>Date</label>
<input type=\"date\" name=\"dateto1\" id=\"dateto1\" class=\"form-control\">
</div>
JAVASCRIPT:
<script type="text/javascript">
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 = yyyy + '/' + mm + '/' + dd;
today = mm + '/' + dd + '/' + yyyy;
console.log(today);
document.getElementById('dateto1').value = today;
}
window.onload = function() {
getDates();
};
</script>
this code is working fine if i use input type "text" but for type date its not working.Please guide me to solve this issue.
Thanks
You should use the yyyy-MM-dd format:
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 = yyyy + '/' + mm + '/' + dd;
today = yyyy + '-' + mm + '-' + dd;
console.log(today);
document.getElementById('dateto1').value = today;
<div class="col-lg-4">
<label>Date</label>
<input type="date" name="dateto1" id="dateto1" class="form-control"></div>
The problem may be with the date format Use yyyy-mm-dd format.
Also there is no getDates function in the code
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 = yyyy + '/' + mm + '/' + dd;
today = `${yyyy}-${mm}-${dd}`;
document.getElementById('dateto1').value = today;
<div class="col-lg-4">
<label>Date</label>
<input type="date" name="dateto1" id="dateto1" class="form-control">
</div>

How to use alert if the system date is changed?

How can I use alert if the date of system is changed in javascript. The date can be change manually of automatically.
[Thought is, Mainly client want to get an notification after day of end and start of the day. Total sales of today will be notified to the client when tomorrow is starting. like if the date of 30 and when it will be 31 and mail will be send to the client that you have sold sum quantity yesterday or date of 30]
Here I have tried this code. I am new in javascript so suggestion is needed.
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 = mm + '/' + dd + '/' + yyyy;
$("#today").change(function () {
alert(today);
});
You cannot listen to the event like you a trying, the only way you can do this is with a synthetic event that is fired when the date changes:
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
}
var changedate = function(date, to){
date = to;
document.trigger('dateChange');
}
document.on('dateChange', function () {
alert(today);
});

javascript date format conversion [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I am using pikaday's datepicker. It gives me output "Fri Sep 20 2013". How can I convert this date into yyyy-mm-dd format and I also would want following date of this selected date and set that one to another element.
I tried this code
function formattedDate() {
var fromdate = new Date(document.getElementById('datepicker').value);
var dd = fromdate.getDate();
var mm = fromdate.getMonth()+1; //January is 0!
var yyyy = fromdate.getFullYear();
if(dd < 10)
{
dd = '0'+ dd;
}
if(mm < 10)
{
mm = '0' + mm;
}
var fromdate1 = dd+'/'+mm+'/'+yyyy;
fromdate.setDate(fromdate.getDate() + 2);
document.getElementById('datepicker').value = fromdate1;
var newdate = fromdate;
document.getElementById('datepicker1').value = newdate;
//alert(newdate1);
}
But it doesn't work.
var date = new Date(dateString);
var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var properlyFormatted = "" + year + month + day;
Or
var date = new Date(dateString);
var properlyFormatted = date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2);
Use momentjs — it's a library available for using in browser and node projects
In your case you should use this pattern:
moment().format("YYYY-mm-D");
And you can try it in console on momentjs's site:

Categories

Resources