web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Convert Text to datetime
Power Automate
Unanswered

Convert Text to datetime

(1) ShareShare
ReportReport
Posted on by 43

hi , I m trying to convert a text to datetime but getting error .please see attached code.

 

SET benPlanExpDate TO TransactionItemData['benPlanExpDate']
Text.ConvertTextToDateTime.ToDateTimeCustomFormat Text: benPlanExpDate CustomFormat: $'''M/d/yyyy 12:00:00 AM''' DateTime=> TextAsDateTime
Text.ConvertDateTimeToText.FromCustomDateTime DateTime: TextAsDateTime CustomFormat: $'''M/dd/yyyy''' Result=> FormattedDateTime

 

kanha123_456_0-1719986686915.png

here in 4 line i am taking this value from queue and in right side output panel showing text  result as '6/1/2024 12:00:00 AM'

its in M/d/yyyy 12:00:00 tt format ..... I am tring to convert this text in datetime its give me error in line 5

kanha123_456_1-1719986824811.png

 

I have the same question (0)
  • VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at

    Hi @kanha123_456 ,

     

    The format in the custom format what you have given is wrong.Please follow the below format while your converting dates.

     

    Formar:

    dd/MM/yyyy HH:mm:ss tt

    MM/dd/yyyy HH:mm:ss tt

     

    (Note:- if you got your solution you can mark as solution and gives kudos)


    Thanks & Regards

    Vishnu Reddy

  • NathanAlvares24 Profile Picture
    1,714 Moderator on at

    Hi @kanha123_456 !

     

    Please use this format:

    NathanAlvares24_0-1719987352325.png

     

     

    M/d/yyyy 12:00:00 tt

     

    But the above will only work for that particular date as "6/1/2024 12:00:00 AM". But if it becomes "11/16/2024 12:00:00 AM" then it will not work. Make your date into standard format and then use this expression:

    NathanAlvares24_0-1719987589782.png

    MM/dd/yyyy 12:00:00 tt

     

     

    I hope this helps.

  • kanha123_456 Profile Picture
    43 on at

    @VishnuReddy1997 @NathanAlvares24 MM/dd/yyyy 12:00:00 tt getting error ...bcse if its one digit date and month is coming as 6/1/2024 12:00:00 AM not as 06/01/2024 12:00:00 AM

  • Verified answer
    VishnuReddy1997 Profile Picture
    2,656 Super User 2025 Season 2 on at

    Hi @kanha123_456 ,

     

    Please find the solution. As your input date and time is in 12 hours format if you use HH:mm:ss it wont work you need to use as hh:mm:ss tt.

    VishnuReddy1997_0-1719989391055.png

     

    VishnuReddy1997_1-1719989413696.png

     

    Format: 

    M/d/yyyy hh:mm:ss tt

     

    Code:

    SET benPlanExpDate TO $'''6/1/2024 12:00:00 AM'''
    Text.Trim Text: benPlanExpDate TrimOption: Text.TrimOption.Both TrimmedText=> benPlanExpDate
    Text.ConvertTextToDateTime.ToDateTimeCustomFormat Text: benPlanExpDate CustomFormat: $'''M/d/yyyy hh:mm:ss tt''' DateTime=> TextAsDateTime

     

    (Note:- if you got your solution you can mark as solution and gives kudos)


    Thanks & Regards

    Vishnu Reddy

  • NathanAlvares24 Profile Picture
    1,714 Moderator on at

    Hi @kanha123_456 !

     

    Two methods I propose:

    Method 1:

    Just fix the format like below:

    NathanAlvares24_0-1719989956023.png

    M/d/yyyy hh:mm:ss tt

     

    Method 2:

    Do it using this simple approach. Just one PowerShell action to fix everything.

     

    Incase your variable comes in as "6/1/2024 12:00:00 AM", we can use PowerShell to put 0 in for the month and date in front. I have just added one action only and PowerShell will do the magic for you. Use this flow:

    NathanAlvares24_0-1719989399379.png

     

    PowerShell script:

    NathanAlvares24_1-1719989419892.png

     

     

    # Original date string
    $originalDateString = "%TransactionItemData%"
    
    # Convert the original string to a DateTime object
    $dateTime = [datetime]::ParseExact($originalDateString, "M/d/yyyy h:mm:ss tt", $null)
    
    # Reformat the DateTime object to the desired format
    $formattedDateString = $dateTime.ToString("MM/dd/yyyy hh:mm:ss tt")
    
    # Output the formatted date string
    $formattedDateString

     

     

     

     

    Full code of the flow (just copy paste this into your flow):

     

     

    SET TransactionItemData TO $'''6/1/2024 12:00:00 AM'''
    @@copilotGeneratedAction: 'False'
    Scripting.RunPowershellScript.RunPowershellScript Script: $'''# Original date string
    $originalDateString = \"%TransactionItemData%\"
    
    # Convert the original string to a DateTime object
    $dateTime = [datetime]::ParseExact($originalDateString, \"M/d/yyyy h:mm:ss tt\", $null)
    
    # Reformat the DateTime object to the desired format
    $formattedDateString = $dateTime.ToString(\"MM/dd/yyyy hh:mm:ss tt\")
    
    # Output the formatted date string
    $formattedDateString''' ScriptOutput=> TransactionItemData ScriptError=> ScriptError
    Text.Trim Text: TransactionItemData TrimOption: Text.TrimOption.Both TrimmedText=> TransactionItemData
    SET benPlanExpDate TO TransactionItemData
    Text.ConvertTextToDateTime.ToDateTimeCustomFormat Text: benPlanExpDate CustomFormat: $'''MM/dd/yyyy 12:00:00 tt''' DateTime=> TextAsDateTime
    Text.ConvertDateTimeToText.FromCustomDateTime DateTime: TextAsDateTime CustomFormat: $'''M/dd/yyyy''' Result=> FormattedDateTime

     

     

     

     

    This will work for both formats:

    "6/1/2024 12:00:00 AM"

    "11/16/2024 12:00:00 AM"

     

    Output:

    For "6/1/2024 12:00:00 AM"

    NathanAlvares24_3-1719989615035.png

     

    For "11/16/2024 12:00:00 AM"

    NathanAlvares24_2-1719989589324.png

     

    I hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard