Set date/time using ADB shell
Asked Answered
B

13

49

I'm trying to set the date/time using the ADB shell but the shell only returns the current time.

I've tried:

adb shell date -s YYYYMMDD.HHmmss

and unix time like:

adb shell date 1318349236

any ideas?

Boardwalk answered 21/10, 2013 at 14:12 Comment(2)
First one should work. Can you write actual command (with replaced YYYYMMDD.HHmmss values) you have sent to device?Stauffer
Possible duplicate of Cannot run adb shell "date `date +%m%d%H%M%Y.%S`"Hoodoo
J
60

To save storage space Android like many other embedded systems uses multi-call binaries to implement its basic command line tools like date.

Android device may include either toolbox or toybox (or both) binary depending on the version. You can check which implementation of the date tool available on your device by running toolbox date and toybox date commands. Then you can use the one which prints out the current date. For example for an Android 6.0+ device it might look like:

$ adb shell toybox date
Mon Jul 31 21:09:28 CDT 2017

$ adb shell toolbox date
date: no such tool

To set date and time using toolbox date use YYYYMMDD.HHmmss format:

adb shell "su 0 toolbox date -s 20161231.235959"

In case of toybox date use MMDDhhmm[[CC]YY][.ss] format:

adb shell "su 0 toybox date 123123592016.59"
Jubilant answered 21/10, 2013 at 14:42 Comment(5)
make sure you have rootJubilant
Works for me... Today was 20140222.014100Oversubtlety
If you want to set the date to the current datem then: adb shell "su 0 date -s `date +%Y%m%d.%H%M%S`"Pebble
It works perfectly having root thanks (make sure you allowed first shell to become root, for example allow it under SuperSU or Superuser apps).Glyptodont
adb shell "su 0 toybox date $(date +%m%d%H%M%Y.%S)" works well for meInflict
H
43

Android 6.0 has a new date format:

Default SET format is "MMDDhhmm[[CC]YY][.ss]", that's (2 digits each)
month, day, hour (0-23), and minute. Optionally century, year, and second.

And setting with -s no longer works. This is the updated set command:

Example of the updated command:

(while inside an adb shell)

date 060910002016.00                              

will result in:

Thu Jun  9 10:00:00 GMT 2016

Notice: The command will not be visible immediately on the device because it doesn't trigger any time change broadcast, But it will be visible within a minute.

To work around that, you can append this broadcast manually this way:

date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET

To call this with an adb command:

adb shell 'date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET'
Hulbig answered 9/6, 2016 at 22:11 Comment(10)
-u means to always use UTC. To set the time in the context of the current time zone, just use "date 060910002016.00" on Marshmallow.Pituitary
@tristan2468 -- thank you! corrected (removed the unneeded -u)Hulbig
Thanks @uval, exactly what i needed!Cyanocobalamin
I can confirm this works on Android Things devpreview 4.1 which is based on Nougat date 081812242017.40 with root permission, otherwise error said date: cannot set date: Operation not permitted Fri Aug 18 12:24:10 GMT 2017Harvin
Sorry, but the new date format is somewhat weird, especially when the ISO format YYYY-MM-DD HH:MM:SS is ubiquitous. I'm wondering when they start to require seconds since the EPOCH...Ivonne
@user3806649 in which scenario? This question is about ADB shell. If you can't find an answer on StackOverflow - open a dedicated question for it.Hulbig
/system/bin/sh: date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SET: not foundNoles
@DivyanshuKushwaha note the "(while inside an adb shell)"Hulbig
@auval Thanks, the command worked without commas i.e. adb shell date 060910002016.00 ; am broadcast -a android.intent.action.TIME_SETNoles
I also needed to adb shell hwclock -w to persist the system time to the rtc (android 8.1)Besmear
K
35

You have to put date value if you want to change it.

-s changes SET format. Default SET format is MMDDhhmm[[CC]YY][.ss].

I successfully tested the following commands:

adb root
adb shell "date `date +%m%d%H%M%Y.%S`"
Karli answered 29/2, 2016 at 23:33 Comment(5)
Thanks, exactly what I needed!Catechism
I made an alias based on this, with su 0 instead of root: alias adb-set-time='adb shell su 0 "date `date +%m%d%H%M%Y.%S`"'Mutinous
created a function instead: function adb-set-time { adb $@ shell su 0 "date `date +%m%d%H%M%Y.%S`"; }. Usage: adb-set-time or adb-set-time -e etcMutinous
This should be set as the answer.Bandaid
date: cannot set date: Operation not permittedPanter
M
16
adb shell date -s `date +%G%m%d.%H%M%S`

At first it gets the current date of your machine and set it on the android target. This approach should work on Linux/Mac/Cygwin.

Magness answered 9/1, 2015 at 15:12 Comment(1)
Try also adb shell date -s "date +'%G%m%d %H:%M:%S'"Magness
I
12

For Android 6+, To change DateTime on Emulator:

Step 1

adb root

Step 2

adb shell date MMDDhhmm[[CC]YY][.ss]

For Example to set date to Oct 29 02:01:55 2020 (or 10/29/2020 02:01:55AM) we do this:

adb shell date 102902012020.55

Explanation of Date Format:

10129202301420205.556

  1. Month MM = 10
  2. Day DD = 29
  3. Hour hh = 02
  4. Minute mm = 01
  5. Year [[CC]YY] = 2020 (or can be 20)
  6. Second [.ss] = .55

*Year and Seconds are optional.

Itemized answered 29/10, 2020 at 16:19 Comment(1)
This was truly helpful, thanks appreciated. (y)Laurice
B
4

On some devices like RTAndroid maybe it works too:

adb shell "su 0 date `date +%m%d%H%M%Y.%S`"
Bookstall answered 20/4, 2017 at 1:43 Comment(0)
J
3

To make this work on my Xperia S, I had to break the commands as follows:

> adb shell
# su -
# date /* see current date */
# date -s YYYYmmdd

Motive: my device had reverted to the beginning of Linux time, and I wasn't particularly worried with time, all I wanted was to set the correct date -- which, BTW I couldn't do through system settings because my custom MIUI ROM kept crashing...

Jumbled answered 11/12, 2014 at 15:17 Comment(0)
R
2

I didn't have any issue with Android 5, most answers I found without su in the command works.

Android 6 is the one that got my attention. On Android 6, it didn't give me error, it just didn't set it right, it will set it back to what it was before the command. I also ran into /system/bin/sh: su: not found errors when I tried this https://mcmap.net/q/349420/-set-date-time-using-adb-shell and this https://mcmap.net/q/349420/-set-date-time-using-adb-shell

After all the try-and-error sessions, I figured it out.

This is the date format I use MMDDhhmm[[CC]YY][.ss]

adb shell date '0739010002017.00'

This command didn't work, initially, in order work, it didn't work by itself.

I used this 3 steps and it works for me.

First, adb shell settings put global auto_time 1

(Turn on Automatic date & time)

Second, adb shell date '0739010002017.00'

( set time you want )

Third, adb shell settings put global auto_time 0

(Turn off Automatic date & time)

Noted: For my specific project, I can not rely on the network-provided and GPS-provided time. That's why I have the third command. which turn off the feature.

Rotund answered 30/7, 2019 at 6:44 Comment(0)
M
1

Expanding on @uval's answer, you can use the following to update the date and time on the android device based on the time on your Windows machine:

set dateYYYY=%date:~10,4%
set dateMM=%date:~4,2%
set dateDD=%date:~7,2%
set timeHH=%time:~0,2%
set timeMM=%time:~3,2%
set timeSS=%time:~6,2%

adb shell su -c date %dateMM%%dateDD%%timeHH%%timeMM%%dateYYYY%.%timeSS%
adb shell su -c am broadcast -a android.intent.action.TIME_SET

Tested on a rooted Android 6.0 device and a Windows 10 PC.

Milky answered 18/4, 2017 at 20:53 Comment(0)
B
0

Using the other answers for the current date I get the following error:

date: bad date '041009502020.23'; Fri Apr 10 09:50:23 CET 2020 != Fri Apr 10 10:50:23 CEST 2020

Using the format @%s (%s being the UNIX timestamp) that the command accepts aswell:

date: bad date '041009532020.32'; Sun Apr 10 09:53:32 CET 2020 != Fri Apr 10 10:53:32 CEST 2020

Turns out this has something to do with timezones, specifically it seems to have to do with DST summertime (since when I replace 04 with 03 (the month) it works just fine).

The solution to this problem is to specify the -u option like so:

# toybox date -u 041009502020.23                                     
Fri Apr 10 09:50:23 UTC 2020

Depending on your timezone this will set the time to a slightly off value, but after you ran that command you can check with the date command to see how many hours you are off and simply account for that in the set date command!

Brittaniebrittany answered 10/4, 2020 at 8:1 Comment(0)
D
0

On AndroidTV API 28 simulator, this works (GNU/Linux, and some Mac) :

adb shell su root date $(date +%m%d%H%M%Y.%S)

The host timezone should match your AndroidTV timezone.

Copy from my answer at : https://mcmap.net/q/265143/-emulator-39-s-clock-doesn-39-t-match-the-host-system-clock

Dantzler answered 8/8, 2023 at 17:59 Comment(0)
M
0

can't add comment:

1, ISO 8601 format also works (see Supported input formats:)

adb shell date '1970-01-17 14:08:09'

adb shell toybox date '1970-01-17 14:08:09'

 # date --help
Toybox 0.8.4-android multicall binary: https://landley.net/toybox (see toybox --help)

usage: date [-u] [-I RES] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-D SET_FORMAT] [SET]

Set/get the current date/time. With no SET shows the current date.

-d      Show DATE instead of current time (convert date format)
-D      +FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
-I RES  ISO 8601 with RESolution d=date/h=hours/m=minutes/s=seconds/n=ns
-r      Use modification time of FILE instead of current date
-u      Use UTC instead of current timezone

Supported input formats:

MMDDhhmm[[CC]YY][.ss]     POSIX
@UNIXTIME[.FRACTION]      seconds since midnight 1970-01-01
YYYY-MM-DD [hh:mm[:ss]]   ISO 8601
hh:mm[:ss]                24-hour time today

All input formats can be followed by fractional seconds, and/or a UTC
offset such as -0800.

All input formats can be preceded by TZ="id" to set the input time zone
separately from the output time zone. Otherwise $TZ sets both.

+FORMAT specifies display format string using strftime(3) syntax:

%% literal %             %n newline              %t tab
%S seconds (00-60)       %M minute (00-59)       %m month (01-12)
%H hour (0-23)           %I hour (01-12)         %p AM/PM
%y short year (00-99)    %Y year                 %C century
%a short weekday name    %A weekday name         %u day of week (1-7, 1=mon)
%b short month name      %B month name           %Z timezone name
%j day of year (001-366) %d day of month (01-31) %e day of month ( 1-31)
%N nanosec (output only)

%U Week of year (0-53 start Sunday)   %W Week of year (0-53 start Monday)
%V Week of year (1-53 start Monday, week < 4 days not part of this year)

%F "%Y-%m-%d"   %R "%H:%M"        %T "%H:%M:%S"        %z  timezone (-0800)
%D "%m/%d/%y"   %r "%I:%M:%S %p"  %h "%b"              %:z timezone (-08:00)
%x locale date  %X locale time    %c locale date/time  %s  unix epoch time

2, for another version date

adb shell toybox date -s '1970-01-17 14:08:09'

# date --help
Toybox 0.8.9-android multicall binary (see toybox --help)

usage: date [-u] [-I RES] [-r FILE] [-d DATE] [+DISPLAY_FORMAT] [-D SET_FORMAT] [SET]

Set/get the current date/time. With no SET shows the current date.

-d      Show DATE instead of current time (convert date format)
-D      +FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
-I RES  ISO 8601 with RESolution d=date/h=hours/m=minutes/s=seconds/n=ns
-r      Use modification time of FILE instead of current date
-s DATE Set the system clock to DATE.
-u      Use UTC instead of current timezone

Supported input formats:

MMDDhhmm[[CC]YY][.ss]     POSIX
@UNIXTIME[.FRACTION]      seconds since midnight 1970-01-01
YYYY-MM-DD [hh:mm[:ss]]   ISO 8601
hh:mm[:ss]                24-hour time today

All input formats can be followed by fractional seconds, and/or a UTC
offset such as -0800.

All input formats can be preceded by TZ="id" to set the input time zone
separately from the output time zone. Otherwise $TZ sets both.

+FORMAT specifies display format string using strftime(3) syntax:

%% literal %             %n newline              %t tab
%S seconds (00-60)       %M minute (00-59)       %m month (01-12)
%H hour (0-23)           %I hour (01-12)         %p AM/PM
%y short year (00-99)    %Y year                 %C century
%a short weekday name    %A weekday name         %u day of week (1-7, 1=mon)
%b short month name      %B month name           %Z timezone name
%j day of year (001-366) %d day of month (01-31) %e day of month ( 1-31)
%N nanosec (output only)

%U Week of year (0-53 start Sunday)   %W Week of year (0-53 start Monday)
%V Week of year (1-53 start Monday, week < 4 days not part of this year)

%F "%Y-%m-%d"   %R "%H:%M"        %T "%H:%M:%S"        %z  timezone (-0800)
%D "%m/%d/%y"   %r "%I:%M:%S %p"  %h "%b"              %:z timezone (-08:00)
%x locale date  %X locale time    %c locale date/time  %s  unix epoch time
Motmot answered 19/7 at 16:49 Comment(0)
O
-2

The correct format that has worked for me is yyyyMMddHHmm.ss

Overexcite answered 30/1, 2014 at 3:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.