Skip to content

Commit

Permalink
Add date formatting to StoryCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
lezhangxyz committed Oct 10, 2012
1 parent 98a2ee0 commit feafda4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/js/modules/StoryCollection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ StoryCollection = (Backbone) ->
parse: (response) ->
# News story models are under 'objects' in the JSON data
return response.objects
parseDate: (unixDate) ->
date = new Date(unixDate)

dateStrings = date.toLocaleDateString().split ", "
# toLocaleDateString() returns in format: "DAY, MONTH DATE, YEAR"
day = dateStrings[0]
month = dateStrings[1].split " "
year = dateStrings[2]

dateNum = month[1]
month = month[0].substr 0,3

time = date.toLocaleTimeString()
# time is in format "HH:MM:SS"
hour = parseInt time.substr 0,2
minutes = time.substr 3,2
if hour > 12
hour -= 12
period = "p.m."
else
period = "a.m."

return "#{hour}:#{minutes} #{period} #{day}, #{month}. #{dateNum}, #{year}"

# Expose only the getInstance function
return getInstance: ->
# Use a singleton pattern for global data access
Expand Down

0 comments on commit feafda4

Please sign in to comment.