cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Post Prodigy
Post Prodigy

Convert text to date value YYMMDD

Hi all..

 

I look everywhere and i haven't found how exactly how to convert a text type into a certain date value YYMMDD.

So, i have this input text box that limit the user to only input exact 6 digit of YYMMDD and store it in a variable, like this:
Set(VarExpiryDate, TextInput_ExpDateManual.Text);
date4.png

And then, we need to compare if the inputted date in the text box above, is already expire or not:

If(DateDiff(Now(), DateValue(Text(DateValue(VarExpiryDate, "en-US"), "[$-en-US]yymmdd")), Months)<3,
Notify("Item will be expired in 3 month", NotificationType.Warning)
;
Set(varEditComment, "Item will be expired in 3 month: [" & Text(VarExpiryDate, "[$-en-US]yymmdd") & "]"
);

If(DateDiff(Now(), DateValue(VarExpiryDate), Months)<1,
Notify("Item will be expired Soon", NotificationType.Error)
;
Set(varEditComment, "Item will be expired soon: [" & Text(VarExpiryDate, "[$-en-US]yymmdd") & "]"
);

If(DateDiff(Now(), DateValue(VarExpiryDate), Days)>1,
Notify("Item already expired", NotificationType.Error)
;
Set(varEditComment, "Item already expired: [" & Text(VarExpiryDate, "[$-en-US]yymmdd") & "]"
);
)
)
);

Problem is, i cannot get the right format to compare with If(DateDiff(Now(), DateValue(VarExpiryDate), Months)<1,
This is what i got so far:
date5.png

How can i convert my YYMMDD text format into the same date format with Now() function so, i can compare them?

Please help.

 

Regards,
pytbyt

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Advocate III
Advocate III

Re: Convert text to date value YYMMDD

You need to pay attention to what variable type is actually being stored.  Your initial formula is storing a string, which can't be evaluated as a date for use in DateDiff without first converting using the DateValue() function.

Try something like this instead:

If(Len(TextInput1.Text) = 6,

UpdateContext({
cvExpYear:Left(TextInput1.Text,2),
cvExpMonth:Mid(TextInput1.Text,3,2),
cvExpDay:Right(TextInput1.Text,2)
});
UpdateContext({
cvExpirationDate: DateValue(cvExpMonth & "/" & cvExpDay & "/" & cvExpYear,"en-Us")
});
UpdateContext({
cvExpiredDays: DateDiff(cvExpirationDate,Now(),Days)
});
If(cvExpiredDays > 0,
UpdateContext({
cvMessage: "Item is EXPIRED by " & cvExpiredDays & " days."
});
,
UpdateContext({
cvMessage: "Item will expire in " & cvExpiredDays *-1 & " days."
});
)
,

UpdateContext({
cvExpYear:Blank(),
cvExpMonth:Blank(),
cvExpDay:Blank(),
cvExpirationDate:Blank(),
cvExpiredDays:Blank(),
cvMessage:"Please enter a date in YYMMDD format!"

})


);

I would suggest adding additional error handling.  I added the above code to (the OnSelect property) the button visible below, but you could put it on the OnChange property for the text input box as well.  I find that seeing the red x on a button is more reliable for seeing errors, so i prefer to go that route during active development.

 

View solution in original post

10 REPLIES 10
Highlighted
Super User
Super User

Re: Convert text to date value YYMMDD

@PytByt  Try

 

Month ( Now() ) - Value( Mid ( VarExpiryDate, 3,2) ) 

In your example this would be

11 (Nov) - 3 = 8

 

This maybe a better option than trying to mess around with date and text types?

 

 

Highlighted
Post Prodigy
Post Prodigy

Re: Convert text to date value YYMMDD

Hi @Eelman 

Could explain me where exactly should i put your formula in my formula above?

Thank you.

Highlighted
Post Prodigy
Post Prodigy

Re: Convert text to date value YYMMDD

hi @Eelman 

I tried to use the formula like this:
If(Month(Now()) - Value(Mid(VarExpiryDate, 3,2))<1,
Notify("Item will be expired Soon", NotificationType.Error);
Set(varEditComment, "Item will be expired soon: [" & Text(VarExpiryDate, "[$-en-US]yymmdd") & "]")

But when i try to use set the variable VarExpiryDate value to: 190301, i dont have my notification showing up.
Although there's also no error message showing.

What went wrong?
Also, should we put the year too? so we can defined the exact expire date?

Please help.

Regards,
pytbyt

Highlighted
Advocate III
Advocate III

Re: Convert text to date value YYMMDD

You need to pay attention to what variable type is actually being stored.  Your initial formula is storing a string, which can't be evaluated as a date for use in DateDiff without first converting using the DateValue() function.

Try something like this instead:

If(Len(TextInput1.Text) = 6,

UpdateContext({
cvExpYear:Left(TextInput1.Text,2),
cvExpMonth:Mid(TextInput1.Text,3,2),
cvExpDay:Right(TextInput1.Text,2)
});
UpdateContext({
cvExpirationDate: DateValue(cvExpMonth & "/" & cvExpDay & "/" & cvExpYear,"en-Us")
});
UpdateContext({
cvExpiredDays: DateDiff(cvExpirationDate,Now(),Days)
});
If(cvExpiredDays > 0,
UpdateContext({
cvMessage: "Item is EXPIRED by " & cvExpiredDays & " days."
});
,
UpdateContext({
cvMessage: "Item will expire in " & cvExpiredDays *-1 & " days."
});
)
,

UpdateContext({
cvExpYear:Blank(),
cvExpMonth:Blank(),
cvExpDay:Blank(),
cvExpirationDate:Blank(),
cvExpiredDays:Blank(),
cvMessage:"Please enter a date in YYMMDD format!"

})


);

I would suggest adding additional error handling.  I added the above code to (the OnSelect property) the button visible below, but you could put it on the OnChange property for the text input box as well.  I find that seeing the red x on a button is more reliable for seeing errors, so i prefer to go that route during active development.

 

View solution in original post

Highlighted
Helper V
Helper V

Re: Convert text to date value YYMMDD

This is a nice solution.

 

Can I ask why you aren't using a date picker to get the date input? 

 

 

Highlighted
Post Prodigy
Post Prodigy

Re: Convert text to date value YYMMDD

Hi @goobernoodles 

I'm still on my way out of town heading back to the HQ, i will try your solution as soon as i arrived. Thank in advance!

Hi @Digital 

If you ask me, i only get the text value which i want to convert into date format form the barcode scanner control. Its the situation that i cant alter.

Cheers.

 

 

Ps.
I may have follow up questions for i am noob in powerapps programming.

Highlighted
Super User
Super User

Re: Convert text to date value YYMMDD

@PytByt  That's because 11-3=8 and 8 isn't less than 1 hence no error output. And yes, you should handle Years as well.

Having a second look, maybe try setting your initial variable to the date format in a string that you need? Like this:

Set(VarExpiryDate,
Concatenate( Right( TextInput_ExpDateManual.Text, 2), "/",
Mid ( TextInput_ExpDateManual.Text, 3, 2 ), "/",
( Value( Left ( TextInput_ExpDateManual.Text, 2) ) + 2000 ) )

Im not sure if the bit above that converts the Year will convert it to a string or number. I think Concatenate will convert it to a string but you may need to mess around with putting the Year convert into a hidden label?

Note, the '+ 2000' assume you are only working with dates >=2000 and <3000.

 

Then in your If():

If( DateDiff ( Now(), DateValue(VarExpiryDate), Months) < 1 .....

 

Also, please try a future date, not one in the past. Comparing Now() to an older date (ie 190301) won't show any errors for you.

 

Hope this helps 🙂 

Post Prodigy
Post Prodigy

Re: Convert text to date value YYMMDD

Hi @Eelman 

You were right.

I also use this modified formula:
UpdateContext({VarYear: "20"& Left(TextInput_ExpDateManual.Text, 2), VarMonth: Mid(TextInput_ExpDateManual.Text, 3,2), VarDay: Right(TextInput_ExpDateManual.Text, 2)})

Thanks for your help!

Cheers,

pytbyt

Highlighted
Post Prodigy
Post Prodigy

Re: Convert text to date value YYMMDD

@goobernoodles 

Thanks for the solution mate!
Fit perfectly!

So, i guess i can use this Context Variable on the different screen with same name? or different name?

Cheers.

pytbyt

Helpful resources

Announcements
secondImage

New Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

Experience what’s next for Power Apps

Join us for an in-depth look at the new Power Apps features and capabilities at the free Microsoft Business Applications Launch Event.

Check this Out

Helpful information

Featuring samples like Return to the Workplace and Emergency Response Applications

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (8,352)