Change date format on linux

convert Linux date to yyyy-MM-dd’T’HH:mm:ss’Z’ format

I need to parameter-ize a datetime value with an objective of passing to a constructed URI to make a Smartsheet API call to get data (i.e. sheets) changed in last 24 hours. I want to use Linux date command as I can do something like date -d ‘1 day ago’ %F to get the output of a day before today. How can I use the date command to convert the value to yyyy-MM-dd’T’HH:mm:ss’Z’ format to get something like 2018-01-01T00:00:00-07:00 ? If the value is not in this particular format, then Smartsheet API complains:

HTTP_01 - Error fetching resource. Status: 400 Reason: Bad Request : < "errorCode" : 1018, "message" : "The value '/home/my/path/to/param_file/Sysdate' was not valid for the parameter modifiedSince.", "refId" : "1xqawd3s94f4y" >

Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: Questions seeking debugging help («why isn’t this code working?») must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example.

4 Answers 4

To output date in ISO 8601 format, you’ll probably want to use -I[FMT] / —iso-8601[=FMT] option, if your date supports it (GNU/Linux version does). The FMT is basically a resolution, and in your case you’ll want to use s for seconds:

$ date -Is 2018-03-09T09:28:14+01:00 

The alternative (for POSIX date , including BSD/OSX date ) is to explicitly specify the format and output the time in UTC time zone ( -u flag):

$ date -u +"%Y-%m-%dT%H:%M:%SZ" 2018-03-09T08:28:14Z 

Note the importance of -u and the explicit Z we can append in that case. Without -u , we would need to output the exact time zone in +hh:mm format, but POSIX date provides support only for time zone name output ( %Z ). GNU date extends the format set with %:z which outputs the numeric time zone, but if already using GNU date , the first approach with -Is is simpler.

Источник

Date Command in Linux: How to Set, Change, Format and Display Date

Linux date command displays and sets the system date and time. This command also allows users to print the time in different formats and calculate future and past dates.

Read on to learn how to use the date command in Linux.

How to use Linux date command

  • A system running Linux
  • A user account with root privileges
  • Access to a terminal window/command line
Читайте также:  Sftp client linux client

Linux date Command Syntax

The syntax for the date command is:

How to Use date Command in Linux

To show the current system time and date, type in the date command:

Date command in linux

The output displays the day of the week, day of the month, month, year, current time, and time zone. By default, the date command is set to the time zone of the operating system.

The -d option allows users to operate on a specific date. For example, we can type in the following command:

See an older date

You can use the —date command to display the given date string in the format of a date. This command does not affect the system’s actual date and time values, and it only prints the requested date. For example:

Pull a date from a string

Note: Learn how can you create a script using the printf command to display the current date.

Linux date Command Format Options

To format the date command’s output, you can use control characters preceded by a + sign. Format controls begin with the % symbol and are substituted by their current values.

Here, the %Y character is replaced with the current year, %m with month, and %d with the day of the month:

date +"Year: %Y, Month: %m, Day: %d"

Format the current date

Here are another two formatting examples:

Another date format example

date +"Week number: %V Year: %y"

Another formatting example

These are the most common formatting characters for the date command:

    • %D – Display date as mm/dd/yy
    • %Y – Year (e.g., 2020)
    • %m – Month (01-12)
    • %B – Long month name (e.g., November)
    • %b – Short month name (e.g., Nov)
    • %d – Day of month (e.g., 01)
    • %j – Day of year (001-366)
    • %u – Day of week (1-7)
    • %A – Full weekday name (e.g., Friday)
    • %a – Short weekday name (e.g., Fri)
    • %H – Hour (00-23)
    • %I – Hour (01-12)
    • %M – Minute (00-59)
    • %S – Second (00-60)

    To see all formatting options, run date —help or the man command man date in your terminal.

    Set or Change Date in Linux

    To change the system clock manually, use the set command. For example, to set the date and time to 5:30 PM, May 13, 2010, type:

    Manual time set in Linux

    Most Linux distributions have the system clock synchronized using the ntp or the systemd-timesyncd services, so be careful when the setting the clock manually.

    Display Past Dates

    Use the —date option to display past dates in Linux. The date command accepts values such as «tomorrow» , «Friday» , «last Friday» , «next Friday» , «next week» , and similar. So, use the following strings to print past dates::

    Format to

    Yesterday

    Time ten seconds ago format in linux

    Display Future Dates

    The —date option can also display future dates. Like with past dates, you can type in strings to print upcoming dates:

    Date next monday format in linux

    Date four days from now format in linux

    Tomorrow

    Display the Date String at Line of File

    The —file option prints the date string present at each line of the file. Unlike the —date option, —file can present multiple date strings at each line.

    This is the syntax for the —file command:

    Here we use the cat command to add dates to a file and then print them with the date command:

    Display dates at each file line in linux

    Display Last Modified Timestamp of a Date File

    When you use the -r option, the date command prints the last modification time of a file. For example, the following command prints the last time the hosts file was changed:

    The last date the file was modified in linux

    Override a Time Zone

    By default, the date command uses the time zone defined in /etc/localtime . To use a different time zone in the environment, set the TZ variable to the desired time zone.

    For example, to switch to New York time, enter:

    Switch to New York time zone in Linux

    Type in the date command to return the system to its default time zone. To see all available time zones, use the timedatectl list-timezones command.

    The date command can also show the local time for a different time zone. For example, to display the local time for 4:30 PM next Monday on the Australian east coast, type:

    date -d 'TZ="Australia/Sydney" 04:30 next Monday'

    Switch to South Australian time in Linux

    Use date with Other Commands

    You can use the date command to create file names that contain the current time and date. The input below creates a backup MySQL file in the format of the current date:

    mysqldump database_name > database_name-$(date +%Y%m%d).sql

    Another common use of the date command is in shell scripts. Below we assign the output of date to the date_now variable:

    Echo the date now variable

    Use Unix Epoch Time (Epoch Converter)

    You can use the date command as an Epoch converter. Epoch, or Unix timestamps, is the number of seconds that have passed since January 1, 1970, at 00:00:00 UTC.

    To show the number of seconds from the epoch to the current day, use the %s format control:

    Epoch seconds in linux

    To see how many seconds passed from epoch to a specific date, enter:

    Epoch seconds from a specific date in Linux

    You now have a good understanding of how to use the date command in Linux. If you are interested in more date/time configuration options for Linux, read How to Set or Change Timezone/Date/Time on Ubuntu.

    Источник

    Changing short date format in Ubuntu

    How do I change the system-wide short date format in Ubuntu? For example, Thunderbird is showing dates in the DD/MM/YY format, and I would like to change it to MM/DD/YY or YYYY-MM-DD. The best information I can find so far is in this thread: http://ubuntuforums.org/showthread.php?t=193916 Edit: I want to change the system-wide date format, so that all my applications use this new date format.

    I’ve written a mini Howto on this. It’s not as easy as I had expected, but shouldn’t pose too many problems.

    4 Answers 4

    • Install and launch «dconf Editor», navigate to com -> canonical -> indicator -> datetime.
    • Set the value of time-format to custom .
    • Customize the Time & Date format by editing the value of custom-time-format , e.g. set it to %Y-%m-%d %H:%M:%S for «2017-12-31 23:59:59» format.
    • Re-login to see effect of the changes.

    You can also do this via a command in terminal:

    gsettings set com.canonical.indicator.datetime time-format 'custom' gsettings set com.canonical.indicator.datetime custom-time-format '%Y-%m-%d %H:%M:%S' 

    This doesn’t exist on my system. The only thing inside of com/canonical is unity , there is no indicator. I’m on Ubuntu 22.04 with Gnome.

    How to do this in 2017 with Ubuntu 16.04 (Xenial Xerus) is described here. Cut/Paste follows below in case that site goes away:

    Change date and measurement formats

    You can control the formats that are used for dates, times, numbers, currency, and measurement to match the local customs of your region.

    • Click the icon at the very right of the menu bar and select System Settings.
    • Open Language Support and select the Regional Formats tab.
    • Select the region that most closely matches the formats you’d like to use. By default, the list only shows regions that use the language set on the Language tab.
    • You have to log out and back in for these changes to take effect. Click the icon at the very right of the menu bar and select Log Out to log out.
    • After you’ve selected a region, the area below the list shows various examples of how dates and other values are shown. Although not shown in the examples, your region also controls the starting day of the week in calendars.

    Источник

    Convert date formats in bash

    I have a date in this format: «27 JUN 2011» and I want to convert it to 20110627 Is it possible to do in bash?

    In case it’s not obvious, the answers with date -d generally apply to GNU date (so, most Linux platforms) while e.g. BSD and thus MacOS support different options and facilities. For complete portability, you want to restrict yourself to POSIX date , which doesn’t really support meaningful conversions between date formats.

    7 Answers 7

    #since this was yesterday date -dyesterday +%Y%m%d #more precise, and more recommended date -d'27 JUN 2011' +%Y%m%d #assuming this is similar to yesterdays `date` question from you #http://stackoverflow.com/q/6497525/638649 date -d'last-monday' +%Y%m%d #going on @seth's comment you could do this DATE="27 jun 2011"; date -d"$DATE" +%Y%m%d #or a method to read it from stdin read -p " Get date >> " DATE; printf " AS YYYYMMDD format >> %s" `date -d"$DATE" +%Y%m%d` #which then outputs the following: #Get date >> 27 june 2011 #AS YYYYMMDD format >> 20110627 #if you really want to use awk echo "27 june 2011" | awk '' | bash #note | bash just redirects awk's output to the shell to be executed #FS is field separator, in this case you can use $0 to print the line #But this is useful if you have more than one date on a line 

    note this only works on GNU date

    Solaris version of date, which is unable to support -d can be resolve with replacing sunfreeware.com version of date

    Источник

Оцените статью
Adblock
detector