I want output like this dd/mm/yyyy

d = Date.parse(“10/12/1995”)
my expected output is exactly like this only dd/mm/yyyy

d = Date.parse("10/12/1995").strftime('%d/%m/%Y')


I cant think of two ways of doing the same. Both look quite similar, and you need to call the strftime method on both objects.

Using Time.strptime :alarm_clock:

require 'time'
Time.strptime('10/12/1995', '%d/%m/%Y').strftime('%d/%m/%Y')    # => "10/12/1995"

Using Date.parse :mantelpiece_clock:

require 'date'
Date.parse('10/12/1995').strftime('%d/%m/%Y')    # => "10/12/1995"

Hope this helps!

if my inputs is 10/12/2019 my expected outputs is dd/mm/yyyy
if my inputs is 2019/12/10 my expected outputs is yyyy/mm/dd
if my inputs is 12/10/2019 my expected outputs is mm/dd/yyyy
like above i want outputs

i want to identify the formate of input date
input: 10-12-1995
output: dd-mm-yyyy

What you’re trying to do is impossible - regardless of programming language.

If you all you have to go on is “10/12/1995” then there is no way of telling whether that’s an American middle-endian date (Month/Day/Year) or a little-endian date (Day/Month/Year). You need some extra information (like a user or locale setting) to decide which one was intended.

To avoid ambiguity, you might consider using ISO format dates 1995-10-12, which can’t be misinterpreted.

1 Like

It is possible and I answered his question

No - it isn’t possible.

What you’ve done is to do it for one particular date, which wasn’t part of the brief.

If one knows the date in advance then yes, it’s trivial as you’ve demonstrated. There is however no way in general to tell whether


means “10th June, 2019” or “6th October, 2019” without some additional information. Americans would generally interpret as being the October date, whilst everyone else would think it is in June.

There are edge cases where you can decide - e.g. if the input is “05/27/2019” then you know it must be a middle-endian date and so the output requested in the spec would be “MM/DD/YYYY”. If you were faced with, say, a collection of 20 or 30 dates which you knew were all in the same format then you could probably decide the format with reasonable certainty.

Faced with one ambiguous date like the one given - “10/12/2019” - there is no way of deciding without some extra information.

1 Like