Skip to content

Commit

Permalink
Added a few simple TILs to Python category even
Browse files Browse the repository at this point in the history
though they where used in jq context.
  • Loading branch information
jonasbn committed Nov 27, 2023
1 parent e91c442 commit 56a824c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@
- [Old-school string formatting](python/old-school_string_formatting.md)
- [What is wheel](python/what_is_wheel.md)
- [How to detect pipe](python/how_to_detect_pipe.md)
- [strptime: Convert a string representing date and time to an object](python/strptime.md)
- [strftime: Convert a DateTime object to a string in a given format](pything/strftime.md)

<a id="qalc"></a>
### qalc - an easy to use command line calculator
Expand Down
30 changes: 30 additions & 0 deletions python/strftime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# strftime

I was working with `jq` when I was reading some documentation which referenced `strftime`, when _googling_ I found this Python resource, which helped me, eventhough I should propably have found one in JavaScript anyway, this one worked for me.

`strftime` can convert a DateTime object to a string in a given format.

```python
from datetime import datetime

now = datetime.now() # current date and time

year = now.strftime("%Y")
print("year:", year)

month = now.strftime("%m")
print("month:", month)

day = now.strftime("%d")
print("day:", day)

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

date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)
```

## Resources and References

- [strftime resource](https://www.programiz.com/python-programming/datetime/strftime)
23 changes: 23 additions & 0 deletions python/strptime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# strptime

I actually used this for a reference for solving some challenges for `jq` and I was reading some documentation which referenced `strftime`, when _googling_ I found this Python resource, which helped me, eventhough I should propably have found one in JavaScript anyway, this one worked for me.

`strptime` can convert a string in a given format to a DateTime object.

```python
from datetime import datetime

date_string = "21 June, 2018"

print("date_string =", date_string)
print("type of date_string =", type(date_string))

date_object = datetime.strptime(date_string, "%d %B, %Y")

print("date_object =", date_object)
print("type of date_object =", type(date_object))
```

## Resources and References

- [strptime resource]https://www.programiz.com/python-programming/datetime/strptime#google_vignette)

0 comments on commit 56a824c

Please sign in to comment.