andrewmcveigh / cljs-time

A clj-time inspired date library for clojurescript.
342 stars 57 forks source link

unparse giving wrong result #66

Closed sonwh98 closed 8 years ago

sonwh98 commented 8 years ago
(ns foo
  (:require [cljs-time.format :as f]
                 [cljs-time.coerce :as c]))

(def date-formatter (f/formatter "MM/DD/YYYY"))
(defn str->date [date-as-str]
  (->> date-as-str (f/parse date-formatter) c/to-date))

(defn date->str [date]
  (->> date c/from-date (f/unparse date-formatter) ))

(-> "06/23/2016" str->date date->str) ;;evaluates to "06/175/2016" when the original  "06/23/2016" is expected

Am I using unparse incorrectly? why does it give "06/175/2016" and not "06/23/2016"?. I am using [org.clojure/clojurescript "1.9.76"]

sonwh98 commented 8 years ago

used moment.js instead

(require  [cljsjs.moment])

(defn str->date [date-as-str]
  (let [m (-> date-as-str (js/moment. "MM/DD/YYYY"))]
    (. m toDate)))

(defn date->str [date]
  (.. (js/moment. date) (format "MM/DD/YYYY")))
andrewmcveigh commented 8 years ago

The pattern DD means day of the year. Lowercase dd is what you want for day of month.

On Fri, 24 Jun 2016 05:14 Sonny To, notifications@github.com wrote:

used moment.js instead

(require [cljsjs.moment])

(defn str->date [date-as-str](let [m %28-> date-as-str %28js/moment.))] (. m toDate)))

(defn date->str [date](.. %28js/moment. date%29 %28format)))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/andrewmcveigh/cljs-time/issues/66#issuecomment-228254365, or mute the thread https://github.com/notifications/unsubscribe/AALnm4T3NA6ywnIJotovG1jHxeoOHZqkks5qO1mggaJpZM4I9cO0 .