0

When I'm trying to run the below command, I'm getting correct output

date --date="04/1/2022 7:1:00" +'%s'

output: 1648776660

But when trying to run the same command with a different date, 29 Jan 2022, it says invalid date:

 date --date="29/1/2022 7:1:00" +'%s'

 error:   date: invalid date ‘29/1/2022 7:1:00’

Is it due to the first part being considered a month? In that case how to handle %s?

as its considering 29th as a month and getting invalid date. Is there any way i can transform from dd-mm-yyyy hh:mm:ss to any other date format so that i can add %s and get the seconds ? Pls any suggestion

4
  • Probably related questions: unix.stackexchange.com/search?q=dd%2Fmm%2Fyyyy+date
    – Kusalananda
    Commented Apr 14, 2022 at 6:45
  • What is your OS?
    – user232326
    Commented Apr 14, 2022 at 7:04
  • @IsaaC .. Thanks for the reply. Its Linux Ubuntu
    – Arya
    Commented Apr 14, 2022 at 7:14
  • Is it possible for you to transform dd-mm-yyyy to yyyy-mm-dd? This is the unambiguous ISO 8601 format and is accepted by date.
    – user516667
    Commented Apr 14, 2022 at 9:03

2 Answers 2

3

The default input format for (GNU) date is the e-mail date format (Please read RFC5322). That format was initially defined in RFC-822 as dd mmm yy. But mmm means the 3 letters (English) month name. As an extension, most date programs also accept numeric mm/dd/yyyy (with month first). Similar to what's defined in POSIX date: mmddhhmm[[cc]yy]

In Ubuntu (your OS) the default date is GNU date, which expect the mm/dd/yyy format. There is no current way (GNU coreutils 8.32) to define other formats.

You can use -D available in busybox date to parse a generic format: Install busybox with sudo apt-get install busybox (after sudo apt-get update -y) in Ubuntu.

$ busybox date -D '%d/%m/%Y %H:%M:%S' --date='04/1/2022 7:1:00'
Tue Jan  4 07:01:00 IST 2022

Assuming you are in India (IST time or +5:30).

Of course, specifying an output format of +'%s' will give you the seconds since epoch. Beware that that will give you the time at IST, not UTC.

$ busybox date -D '%d/%m/%Y %H:%M:%S' --date='04/1/2022 7:1:00 +0530' +'%s'
1641259860

$ date -ud @1641259860
Tue 04 Jan 2022 01:31:00 AM UTC

Note how times are different !!.

For everyone else (not in India time) that result could be reproduced with:

$ TZ='Asia/Kolkata' busybox date -D '%d/%m/%Y %H:%M:%S' --date='04/1/2022 7:1:00' +'%s'
1641259860

In some BSD date implementations we can use the -j and -f options for similar results.

$ date -j -f "%d/%m/%Y %T" "04/1/2022 7:1:00" +"%s"
1641259860

0
1

This has nothing to do with +%s. Are you sure date can parse dd/mm/yyyy format ?

(man page makes no provision to set a LC_TIMEvalue).

In your epoch sample above

LC_TIME=C date -d@1648776660
Fri Apr  1 03:31:00 CEST 2022

were you testing for Jan 4th or Apr 1st? (and time does not match 7:1:00)

My guess is that 29/1/2022 is parsed as the 29th month's 1st day of 2022.


Edit: confirmed using --debug option

$ date --debug --date="29/01/2022 7:1:00" +'%s'

date: warning: value 29 has less than 4 digits. Assuming MM/DD/YY[YY]
date: parsed date part: (Y-M-D) 2022-29-01
date: parsed time part: 07:01:00
date: input timezone: system default
date: using specified time as starting value: '07:01:00'
date: error: invalid date/time value:
date:     user provided time: '(Y-M-D) 2022-29-01 07:01:00'
date:        normalized time: '(Y-M-D) 2024-05-01 07:01:00'
date:                                  ---- --
date:      possible reasons:
date:        numeric values overflow;
date:        missing timezone
date: invalid date ‘29/01/2022 7:1:00’
5
  • Yes your guess is correct. It considering it as 29th Month but not as 29th January. Is there a way we can parse %s with dd/mm/yyyy ?
    – Arya
    Commented Apr 14, 2022 at 5:35
  • I am testing it for 4th January but not April 1st
    – Arya
    Commented Apr 14, 2022 at 5:35
  • Is there any way i can convert this timestamp to mm/dd/yyyy hh:mm:ss and then do %s ?
    – Arya
    Commented Apr 14, 2022 at 5:59
  • @Arya There is no indication in your question that you are locked into using dates on that particular format. If your question is actually about transforming the given date strings to something that GNU date can parse, then this should probably be part of the question.
    – Kusalananda
    Commented Apr 14, 2022 at 6:43
  • @Kusalananda . Thank You. I just added more details in the question.
    – Arya
    Commented Apr 14, 2022 at 6:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .