DareDrop
Posts: 14
Joined: Sun Aug 20, 2017 1:18 am

Datetime issue

Thu Aug 31, 2017 5:05 pm

I am using a DHT22 sensor to log temp and humidity and save it to a csv file.

What I need is a date for each read in this format MM/DD/YYYY

And the time as HH:MM:SS

Been try to work this out, have looked and at ref and still can not get it.

Pls help

Thanks

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Datetime issue

Thu Aug 31, 2017 5:17 pm

What software are you using to read the sensor? What language is it written in? Where did you download it from?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DareDrop
Posts: 14
Joined: Sun Aug 20, 2017 1:18 am

Re: Datetime issue

Thu Aug 31, 2017 5:23 pm

Sorry should have put that in, my bad.

Raspi 3
Raspbain Stetch updated Upgraded
Python

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Datetime issue

Thu Aug 31, 2017 5:52 pm

Post your python code in [code]... your stuff goes here ...[/code] tags. As that makes the difference between

from datetime import datetime

very_True=True

iso_format=True
silly_american_fmt=very_True

date_str = datetime.now()
yyyy_mm_dd = datetime.strftime(date_str, "%Y-%m-%d %H:%M:%S")
american_fmt = datetime.strftime(date_str, "%m/%d/%Y %H:%M:%S")

if silly_american_fmt == True:
print (american_fmt)
else:
print (yyyy_mm_dd)

And

Code: Select all

from datetime import datetime

very_True=True

iso_format=True
silly_american_fmt=very_True

date_str = datetime.now()
yyyy_mm_dd = datetime.strftime(date_str, "%Y-%m-%d %H:%M:%S")
american_fmt = datetime.strftime(date_str, "%m/%d/%Y %H:%M:%S")

if silly_american_fmt == True:
  print (american_fmt)
else:
  print (yyyy_mm_dd)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DareDrop
Posts: 14
Joined: Sun Aug 20, 2017 1:18 am

Re: Datetime issue

Thu Aug 31, 2017 6:32 pm

Post your python code in

Code: Select all

... your stuff goes here ...
tags. As that makes the difference between

from datetime import datetime

very_True=True

iso_format=True
silly_american_fmt=very_True
Not sure what this is, would you explain pls
date_str = datetime.now()
yyyy_mm_dd = datetime.strftime(date_str, "%Y-%m-%d %H:%M:%S")
american_fmt = datetime.strftime(date_str, "%m/%d/%Y %H:%M:%S")
This is the format get set up Date in 2 forms, OK

if silly_american_fmt == True:
print (american_fmt)
else:
print (yyyy_mm_dd)
This is the print statement OK

User avatar
bensimmo
Posts: 4654
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Datetime issue

Thu Aug 31, 2017 6:53 pm

He is showing you and example between using 'code tags' and not using them, it's one of the format option when posting on here.


He wants you to post your code.

DareDrop
Posts: 14
Joined: Sun Aug 20, 2017 1:18 am

Re: Datetime issue

Thu Aug 31, 2017 9:14 pm

Code: Select all

import time
import datetime
import csv
import sys

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 4


with open('dht.csv', 'w') as csv_file:
    dhtwriter=csv.writer(csv_file)
    dhtwriter.writerow(['Time', 'Tempemperature (F)', 'Humidity (%)'])
    while True:
        reading_time = datetime.datetime.now()
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        if humidity is not None and temperature is not None:
            time_now = datetime.datetime.now()
            Tempemperature = ('Temp={0:0.1f}*C')
            print('Time: {0} Tempemperature: {1} Humidity: {2}'.format(time_now, temperature, humidity))
            dhtwriter.writerow([time_now, temperature, humidity])
            time.sleep(600)
Here is the code.

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Datetime issue

Thu Aug 31, 2017 9:48 pm

This shows you how to get the date and time or just the time.

Code: Select all

import time

datetime = time.strftime("%d-%m-%Y %H:%M:%S")
print datetime

time = time.strftime("%H:%M:%S")

print time
results look like this
31-08-2017 22:47:36
22:47:36
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Datetime issue

Fri Sep 01, 2017 12:52 am

Change these lines

Code: Select all

        if humidity is not None and temperature is not None:
            time_now = datetime.datetime.now()
            Tempemperature = ('Temp={0:0.1f}*C')
to

Code: Select all

        if humidity is not None and temperature is not None:
            time_now = datetime.datetime.now()
            time_now = time_now.strftime("%m/%d/%Y %H:%M:%S")
            Tempemperature = ('Temp={0:0.1f}*C')
NOTE that ISO dates (2017-09-01) naturally sort in date order. American dates (09/01/2017) need special handing to sort correctly (Excel can do that, because it converts your format to a day number internally).

BTW, if you want temps in °F rather than °C change

Code: Select all

        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        
to

Code: Select all

        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        temperature = (temperature*1.8)+32
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DareDrop
Posts: 14
Joined: Sun Aug 20, 2017 1:18 am

Re: Datetime issue

Fri Sep 01, 2017 12:54 am

Code: Select all

>>> import time
>>> datetime = time.strftime("%d-%m-%Y %H:%M:%S")
>>> print datetime
SyntaxError: Missing parentheses in call to 'print'

>>> print (datetime)
31-08-2017 20:47:23

>>> time = time.strftime("%H:%M:%S")
>>> print time
SyntaxError: Missing parentheses in call to 'print'

>>> print(time)
20:49:44
This works with the change to the print statement.

Thanks

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Datetime issue

Fri Sep 01, 2017 12:58 am

That's a feechur of python2 code being run with python3.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DareDrop
Posts: 14
Joined: Sun Aug 20, 2017 1:18 am

Re: Datetime issue

Fri Sep 01, 2017 1:52 am

import time
import datetime
import csv
import sys

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 4

with open('dht.csv', 'w') as csv_file:
dhtwriter=csv.writer(csv_file)
dhtwriter.writerow(['Date', 'Time', 'Tempemperature (F)', 'Humidity (%)'])
while True:
reading_time = datetime.datetime.now()
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
sensor_date = time.strftime("%m-%d-%Y")
sensor_time = time.strftime("%H:%M:%S")
Tempemperature = ('Temp={0:0.1f}*C')
Humidity = ('Humidity={1:0.1f}%')
print('Date: {0} Time: {1} Tempemperature: {2} Humidity: {3}'.format(sensor_date,
sensor_time, temperature, humidity))
dhtwriter.writerow([sensor_date, sensor_time, temperature, humidity])
time.sleep(4)
New code

pi@raspisensor:~/Adafruit_Python_DHT/examples $ sudo python simpletest_h.py
Date: 08-31-2017 Time: 21:44:19 Tempemperature: 23.7000007629 Humidity: 44.5
Date: 08-31-2017 Time: 21:44:24 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:28 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:33 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:38 Tempemperature: 23.6000003815 Humidity: 44.9000015259
Date: 08-31-2017 Time: 21:44:45 Tempemperature: 23.6000003815 Humidity: 44.9000015259

output

now i just need to format the numbers.

Thanks

Return to “Beginners”