• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Create new date from old date, keeping day and month entries from old date

Contributor ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

Customer enters their date of birth (oDOB) in mm/dd/yy format in a text field named Date_of_Birth. I want to take this customer-entered oDOB and keep the mm/dd the same but bring the yy to the current year, creating new date named cDOB that consists of the original oDOB dd/mm values associated with the current year's yy value. This allows me to have a current date of birth (cDOB) from the customer's entered date of birth (oDOB). I then want to be able to use the current date of birth (cDOB) in date calculations to determine specific periods of time by adding or subtracting the current date of birth (cDOB) to/from a given date to get the number of days between these two dates. This last part I'm ok with- I have Tom's articles and looked at plenty of examples of date calculations, including try67's substring approach, but I just can't seem to get it- any help greatly appreciated- here's my latest edition of "lost count on how many times and ways tried" script-  thanks in advance- kemper

 

if (dDOB === "") event.value = "";
else {
var TODAY = new Date ();
var currYR = TODAY.getFullYear();//gets current Year
var oDOB = this.getField("Date_of_Birth").valueAsString; //gets birthdate
var bd = new Date(oDOB);//creates birthdate
var bdmonth = bd.getMonth();//gets birthdate month
var bdday = bd.getDate();// gets birthdate day
var bdyear = currYR;// sets oDOB year to current year
var cDOB = new Date(bdmonth,bdday,bdyear);//creates current DOB
event.value = util.printd("mm/dd/yy",cDOB);

}

TOPICS
How to , JavaScript

Views

559

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 28, 2021 Feb 28, 2021

See if this is what you looking for, it will use month and day from "Date_of_Birth" and current year, Just make sure format stays "mm/dd/yy".

var oDOB = this.getField("Date_of_Birth").valueAsString;

if(oDOB == "") event.value = "";
else{
var cDOB = util.scand("mm/dd/yy", oDOB.slice(0,6));
event.value = util.printd("mm/dd/yy", cDOB);}

Votes

Translate

Translate
Contributor ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

sorry, it should be 

if (oDOB === "")  

not

if (dDOB === "")

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

I don't see where you define that variable, though...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

var oDOB = this.getField("Date_of_Birth").valueAsString; //gets birthdate

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

See if this is what you looking for, it will use month and day from "Date_of_Birth" and current year, Just make sure format stays "mm/dd/yy".

var oDOB = this.getField("Date_of_Birth").valueAsString;

if(oDOB == "") event.value = "";
else{
var cDOB = util.scand("mm/dd/yy", oDOB.slice(0,6));
event.value = util.printd("mm/dd/yy", cDOB);}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

YES!!! that was what I was trying to do. Rather than have the resultant date displayed, can I use the variable cBOD in further date calculations or does it have to be changed from .valueAsString to .value? I intend to use it to add and subtract dates from it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2021 Mar 01, 2021

Copy link to clipboard

Copied

LATEST

Yes, you can use it in calculating  years, months, days ...etc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 28, 2021 Feb 28, 2021

Copy link to clipboard

Copied

What should happens with the 29th February?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines