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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Automate
Answered

Date Format

(0) ShareShare
ReportReport
Posted on by 27

I have string which contains the text as 
1 hour ago
5 hours ago
1 day ago
5 days ago 
1 week ago 
5 weeks ago
1 month ago 
2 months ago
1 year ago
5 years ago
I want to convert them according to that date formart.
For suppose, if it is 4 years ago then it should be converted to 28/03/2024
Please give me most efficient and less time taking solution

I have the same question (0)
  • Verified answer
    VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at

    Hi @Raju25 ,

     

    What if it is 1,hour ago,1day ago,1week ago.

    If it is 1year ago it should give as 28-03-2023 right?

     

    Regards,

    Vishnu  Reddy

  • Verified answer
    CU16071609-1 Profile Picture
    6,255 Moderator on at

    @Raju25 

     

    Method1:

    Using PAD Switch and Parse actions

    Deenuji_0-1711626632071.png

     

    Code:

    SET Inputstring TO $'''1 hour ago'''
    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateAndTime CurrentDateTime=> CurrentDateTime
    Text.ParseText.RegexParse Text: Inputstring TextToFind: $'''(\\d+)''' StartingPosition: 0 IgnoreCase: False OccurrencePositions=> Positions Matches=> Matches
    SWITCH Inputstring.Trimmed
     CASE Contains($'''hour''', False)
     DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Matches[0]%''' TimeUnit: DateTime.TimeUnit.Hours ResultedDate=> DateOutput
     CASE Contains($'''day''', False)
     DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Matches[0]%''' TimeUnit: DateTime.TimeUnit.Days ResultedDate=> DateOutput
     CASE Contains($'''month''', False)
     DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Matches[0]%''' TimeUnit: DateTime.TimeUnit.Months ResultedDate=> DateOutput
     CASE Contains($'''year''', False)
     DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Matches[0]%''' TimeUnit: DateTime.TimeUnit.Years ResultedDate=> DateOutput
    END

    Method 2:

    Try to .net solution below suggested. Pass your input into .net scripts and it will return the expected output.

    Deenuji_0-1711623945226.png

     

    .Net code

    Deenuji_1-1711623975737.png

     

    Code:

     

    SET Inputstring TO $'''1 year ago'''
    SET ConvertDatetime TO $'''%''%'''
    Scripting.RunDotNetScript Language: System.DotNetActionLanguageType.CSharp Script: $''' if (input.EndsWith(\"hour ago\") || input.EndsWith(\"hours ago\"))
     {
     int hours = int.Parse(input.Split(\' \')[0]);
     returndatetime=DateTime.Now.AddHours(-hours);
     }
     else if (input.EndsWith(\"day ago\") || input.EndsWith(\"days ago\"))
     {
     int days = int.Parse(input.Split(\' \')[0]);
     returndatetime = DateTime.Now.AddDays(-days);
     }
     else if (input.EndsWith(\"week ago\") || input.EndsWith(\"weeks ago\"))
     {
     int weeks = int.Parse(input.Split(\' \')[0]);
     returndatetime = DateTime.Now.AddDays(-7 * weeks);
     }
     else if (input.EndsWith(\"month ago\") || input.EndsWith(\"months ago\"))
     {
     int months = int.Parse(input.Split(\' \')[0]);
     returndatetime = DateTime.Now.AddMonths(-months);
     }
     else if (input.EndsWith(\"year ago\") || input.EndsWith(\"years ago\"))
     {
     int years = int.Parse(input.Split(\' \')[0]);
     returndatetime = DateTime.Now.AddYears(-years);
     }
    ''' @'name:input': Inputstring @'type:input': $'''String''' @'direction:input': $'''In''' @'name:returndatetime': $'''''' @'type:returndatetime': $'''Datetime''' @'direction:returndatetime': $'''Out''' @returndatetime=> ConvertDatetime

     

     

     
    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀. If you'd like to appreciate me, please write a LinkedIn recommendation 🙏

  • Verified answer
    VishnuReddy1997 Profile Picture
    2,666 Super User 2026 Season 1 on at

    Hi @Raju25 ,

     

    As I Understood from your elaboration I have created a flow. Please find the solution.

    VishnuReddy1997_0-1711624685125.pngVishnuReddy1997_1-1711624725102.png

     

    Code:

     

    SET Extracted_Input TO $'''1 hours'''
    Text.SplitText.Split Text: Extracted_Input StandardDelimiter: Text.StandardDelimiter.Space DelimiterTimes: 1 Result=> Subtracted_Input
    DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateAndTime CurrentDateTime=> CurrentDateTime
    SWITCH Extracted_Input
    CASE Contains($'''hour''', False)
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Subtracted_Input[0]%''' TimeUnit: DateTime.TimeUnit.Hours ResultedDate=> ResultedDate_Output
    CASE Contains($'''day''', False)
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Subtracted_Input[0]%''' TimeUnit: DateTime.TimeUnit.Days ResultedDate=> ResultedDate_Output
    CASE Contains($'''month''', False)
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Subtracted_Input[0]%''' TimeUnit: DateTime.TimeUnit.Months ResultedDate=> ResultedDate_Output
    CASE Contains($'''year''', False)
    DateTime.Add DateTime: CurrentDateTime TimeToAdd: $'''-%Subtracted_Input[0]%''' TimeUnit: DateTime.TimeUnit.Years ResultedDate=> ResultedDate_Output
    END

     

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

     

    Thanks & Regards

    Vishnu Reddy

     

     

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 269 Super User 2026 Season 2

#2
trice602 Profile Picture

trice602 184 Super User 2026 Season 2

#3
11manish Profile Picture

11manish 136 Super User 2026 Season 2

Last 30 days Overall leaderboard