You are viewing limited content. For full access, please sign in.

Question

Question

How to extract day, month & year from input Date Field on change

asked on March 30, 2019

Hi all

 

I am trying to extract DD, MM, YYYY from input date field in a FORM formatted as DD/MM/YYYY upon user change. The field default input value is current_date. I am new to javascript, so I found some helpful posts to get this information using var new date function, but I need to get the right values when User fills the form selecting another date rather than current date.

Here is my start 

$(document).ready(function() {
     var d = new Date();
     var YYYY = d.getFullYear();
     var YY = YYYY.toString().substring(2);
     var MM = ("0" + (d.getMonth() + 1)).slice(-2);
     var DD = ("0" + d.getDate()).slice(-2);
});

Thanks for any help

0 0

Answer

SELECTED ANSWER
replied on March 31, 2019

hello,

 

in order to work with jquery you need to put the code the following way: (with the format desired)

$(document).ready(function(){

$('.calendardate input').change(function(){
  
  var dt = new Date( $(this).val());
  var year = dt.getFullYear();
  var month =  (dt.getMonth() < 10 ? '0' : '') + (dt.getMonth()+1);
  var day = (dt.getDate() < 10 ? '0' : '') + dt.getDate();


  
})
});

 

 

in order to user MM DD orany other format with the formula, try using 

=TEXT(variable,"MM") instead of =MONTH

=TEXT(variable,"DD") instead of =DAY

1 0

Replies

replied on March 30, 2019

Hello,

 

there are 2 methods to do this, and there is one without jquery.

the first methods: 

if you want to put the month, year and day in fields inside the forms, just go to these fields and add in the function section (in the advanced tab) the following respectively:

=Month(Date_variable_name) 

=YEAR(Date_variable_name)

=DAY(Date_variable_name)

this will automatically take the variable on change.

the second method:

with jquery as follows :

  $('.dateclass input').change(function(){
    
    var dt = new Date( $(this).val());
    var year = dt.getFullYear();
    var month = dt.getMonth()+1;
    var day = dt.getDate();
  
    
  })

hope this will help you.

regards,

Maher.

1 0
replied on March 31, 2019

Hi Maher

I tried first method, it works, but returns day as d not dd ( unless day is > 9 , in this case it will retrun two digits). Same results for month. I need to return two digits instead.

Tried the the second method, got no results, I might have missed something

0 0
SELECTED ANSWER
replied on March 31, 2019

hello,

 

in order to work with jquery you need to put the code the following way: (with the format desired)

$(document).ready(function(){

$('.calendardate input').change(function(){
  
  var dt = new Date( $(this).val());
  var year = dt.getFullYear();
  var month =  (dt.getMonth() < 10 ? '0' : '') + (dt.getMonth()+1);
  var day = (dt.getDate() < 10 ? '0' : '') + dt.getDate();


  
})
});

 

 

in order to user MM DD orany other format with the formula, try using 

=TEXT(variable,"MM") instead of =MONTH

=TEXT(variable,"DD") instead of =DAY

1 0
replied on March 31, 2019

Hi Maher

Thanks, it works now.

but have two minor concerns.

1) If input date field is set to default (current date), the results will not show/calculated till the user change (click and select date). why not concidering effective with the initial set default value.

2) If input date field value set to format as dd/MM/yyyy (eg. 1st April 2019 = 01/04/2019  ), the returns of month and day variables are exchanged ( i.e month val returns 01 , day val returns 04 ), all other date format works corrects except this format !

Thanks for your respond

 

0 0
replied on April 1, 2019

Hello,

Glad to hear it!!

i tried it on version :10.2.1.157 and it is working fine without the issues that you are facing.

which version are you working with.

if it i can replicate the error with the same version, i think you should refer back to laserfiche support to report the issue.

 

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.