cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
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

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
Super User
Super User

@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?

 

 

Hi @Eelman 

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

Thank you.

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

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

This is a nice solution.

 

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

 

 

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.

@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 🙂 

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

@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
News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

Power Apps Community Call

Power Apps Community Call- January

Mark your calendars and join us for the next Power Apps Community Call on January 20th, 8a PST

PP Bootcamp Carousel

Global Power Platform Bootcamp

Dive into the Power Platform stack with hands-on sessions and labs, virtually delivered to you by experts and community leaders.

secondImage

Power Platform Community Conference On Demand

Watch Nick Doelman's session from the 2020 Power Platform Community Conference on demand!

Top Solution Authors
Top Kudoed Authors
Users online (5,734)