Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

ISO 8601 date format for microformats

Used this in Ruby to get the ISO 8601 (see RFC 3339) full date format for an hEvent Microformat. Doubtless usable in other situations.

strftime('%Y-%m-%dT%H:%M:%S%z')


How to use:

<abbr class="dtstart" title="<%= event.start_date.strftime('%Y-%m-%dT%H:%M:%S%z') %>"><%= event.start_date.to_s(:long) %></abbr>

Ruby strftime without leading zeros

Date.today.strftime('%b %d, %Y').gsub(/ 0(\d\D)/, ' \1')


Um... strike that. Seems %e does it without the extra work. Thanks Peter!

Formatting time

>>> from e32db import format_time
>>> from time import *
>>> t = time()
>>>
>>> format_time(t)   # for Symbian SQL
u'06/03/2006 23:26:35'
>>> strftime('%d/%m/%Y %H:%M:%S')  # like above
'06/03/2006 23:26:39'
>>> ctime()
'Mon Mar 06 23:26:49 2006'
>>> strftime("%a, %d %b %Y %H:%M:%S +0000")  # email RFC
'Mon, 06 Mar 2006 23:26:59 +0000'

Here's the (shortened) table for strftime.
%a  weekday name.  
%A  weekday name (full).  
%b  month name.  
%B  month name (full).  
%c  date and time (locale)
%d  day of month [01,31].  
%H  hour [00,23].  
%I  hour [01,12].  
%j  day of year [001,366].  
%m  month [01,12].  
%M  minute [00,59].  
%p  AM or PM
%S  Second [00,61]
%U  week of year (Sunday)[00,53].
 w  weekday [0(Sunday),6].
 W  week of year (Monday)[00,53].
 x  date (locale).
%X  time (locale).
%y  year [00,99].
%Y  year [2000].
%Z  timezone name.  

RFC-822 compatible dates in RSS feeds

If you're building RSS 2.0 files with .rxml files on your Rails app, this sort of line will get you a valid date/time:

xml.pubDate(p.updated_at.strftime("%a, %d %b %Y %H:%M:%S %z"))


If you're just using strftime someplace else, perhaps in PHP, try:

strftime("%a, %d %b %Y %H:%M:%S %z")
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS