How can I create basic timestamps or dates? (Python 3.4)

Ultimately you want to review the datetime documentation and become familiar with the formatting variables, but here are some examples to get you started: import datetime print(‘Timestamp: {:%Y-%m-%d %H:%M:%S}’.format(datetime.datetime.now())) print(‘Timestamp: {:%Y-%b-%d %H:%M:%S}’.format(datetime.datetime.now())) print(‘Date now: %s’ % datetime.datetime.now()) print(‘Date today: %s’ % datetime.date.today()) today = datetime.date.today() print(“Today’s date is {:%b, %d %Y}”.format(today)) schedule=”{:%b, %d %Y}”.format(today) + …

Read more

Converting timezone-aware datetime to local time in Python

In general, to convert an arbitrary timezone-aware datetime to a naive (local) datetime, I’d use the pytz module and astimezone to convert to local time, and replace to make the datetime naive: In [76]: import pytz In [77]: est=pytz.timezone(‘US/Eastern’) In [78]: d.astimezone(est) Out[78]: datetime.datetime(2010, 10, 30, 13, 21, 12, tzinfo=<DstTzInfo ‘US/Eastern’ EDT-1 day, 20:00:00 DST>) …

Read more

Get current date in DD-Mon-YYY format in JavaScript/Jquery

There is no native format in javascript for DD-Mon-YYYY. You will have to put it all together manually. The answer is inspired from : How do I format a date in JavaScript? // Attaching a new function toShortFormat() to any instance of Date() class Date.prototype.toShortFormat = function() { const monthNames = [“Jan”, “Feb”, “Mar”, “Apr”, …

Read more

pytz and astimezone() cannot be applied to a naive datetime

For pytz timezones, use their .localize() method to turn a naive datetime object into one with a timezone: start_date = local_tz.localize(start_date) For timezones without a DST transition, the .replace() method to attach a timezone to a naive datetime object should normally also work: start_date = start_date.replace(tzinfo=local_tz) See the localized times and date arithmetic of the …

Read more

The specified value does not conform to the required format yyyy-MM-dd

The specifications for the HTML5 date picker state that the date must be in the format yyyy-MM-dd (ISO format). This means that you DisplayFormatAttribute must be [DisplayFormat(DataFormatString = “{0:yyyy-MM-dd}”, ApplyFormatInEditMode = true)] public string MyDate { get; set; } Alternatively you can manually add the format using @Html.TextBoxFor(m => m.MyDate, “{0:yyyy-MM-dd}”, new { @type = …

Read more

Get AM/PM for a date time in lowercase using only a datetime format

I would personally format it in two parts: the non-am/pm part, and the am/pm part with ToLower: string formatted = item.PostedOn.ToString(“dddd, MMMM d, yyyy a\\t h:mm”) + item.PostedOn.ToString(“tt”).ToLower(); Another option (which I’ll investigate in a sec) is to grab the current DateTimeFormatInfo, create a copy, and set the am/pm designators to the lower case version. …

Read more

Python datetime object show wrong timezone offset

See: http://bytes.com/topic/python/answers/676275-pytz-giving-incorrect-offset-timezone In the comments, someone proposes to use tzinfo.localize() instead of the datetime constructor, which does the trick. >>> tz = timezone(‘Asia/Kolkata’) >>> dt = tz.localize(datetime.datetime(2011, 6, 20, 0, 0, 0, 0)) >>> dt datetime.datetime(2011, 6, 20, 0, 0, tzinfo=<DstTzInfo ‘Asia/Kolkata’ IST+5:30:00 STD>) UPDATE: Actually, the official pytz website states that you should always …

Read more