├── .gitignore ├── docker-compose.yml ├── Dockerfile ├── AUTHORS.md ├── .travis.yml ├── bower.json ├── .github └── FUNDING.yml ├── package.json ├── LICENSE ├── lib └── moment-strftime.js ├── README.md ├── ChangeLog └── spec └── strftime.spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | .roboconf.sh 3 | bower_components/ 4 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | app: 2 | build: . 3 | working_dir: /src 4 | volumes: 5 | - .:/src 6 | command: npm test 7 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:latest 2 | 3 | # I'm floored that `npm install --global` doesn't install devDependencies. That's literally the only thing I really want. 4 | RUN mkdir -p /usr/local/npm 5 | ENV NODE_PATH /usr/local/npm/node_modules 6 | ENV PATH "$PATH:/usr/local/npm/node_modules/.bin" 7 | COPY package.json /usr/local/npm/package.json 8 | WORKDIR /usr/local/npm/ 9 | RUN npm install 10 | 11 | CMD false 12 | -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | In alphabetical order: 2 | 3 | * Benjamin Oakes (@benjaminoakes) 4 | * Brandon Pittman (@brandonpittman) 5 | * David Rojo Gonzalez (@davidrojo) 6 | * Gilmore Davidson (@gilmoreorless) 7 | * Jed Fox (@j-f1) 8 | * Julian Scheid (@jscheid) 9 | * Martin Hradil (@himdel) 10 | * Nicolas (@nikospkrk) 11 | * Paweł Sierszeń (@nix1) 12 | * Pierre Rudloff (@Rudloff) 13 | * Serhiy Yefremenko (@yefrem) 14 | * Ziv Perry (@ziv) 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | matrix: 2 | include: 3 | - language: node_js 4 | node_js: node 5 | - sudo: required 6 | services: 7 | - docker 8 | install: 9 | - docker-compose build 10 | script: 11 | - docker-compose run app npm test 12 | deploy: 13 | provider: npm 14 | email: hello@benjaminoakes.com 15 | api_key: 16 | secure: Zq6OFLLnPcE2Bq4bz6NkBGQg9zNR9HMGaFVK/UgztAen8VV8nQMBj3T2zxu/YAgfinC5wicz0a9oNn7OTmyy0CuH03UeWfdMzGQ8mXvU/iK1Q1UISVkNkrIvJz28zgiUzTZxz2X6T7A3O634vFxcjB715kcyQqbEtnbIv+sStdQ= 17 | on: 18 | tags: true 19 | repo: benjaminoakes/moment-strftime 20 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "moment-strftime", 3 | "version": "0.5.0", 4 | "homepage": "https://github.com/benjaminoakes/moment-strftime", 5 | "authors": [ 6 | "Benjamin Oakes (http://www.benjaminoakes.com/)" 7 | ], 8 | "description": "moment-strftime adds strftime to Moment.js", 9 | "main": "lib/moment-strftime.js", 10 | "keywords": [ 11 | "moment", 12 | "date", 13 | "strftime" 14 | ], 15 | "license": "MIT", 16 | "ignore": [ 17 | "**/.*", 18 | "node_modules", 19 | "bower_components", 20 | "spec" 21 | ], 22 | "dependencies": { 23 | "moment": "^2.11.2" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # # These are supported funding model platforms 2 | # 3 | # github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | # patreon: # Replace with a single Patreon username 5 | # open_collective: # Replace with a single Open Collective username 6 | # ko_fi: # Replace with a single Ko-fi username 7 | # tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | # community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | # liberapay: # Replace with a single Liberapay username 10 | # issuehunt: # Replace with a single IssueHunt username 11 | # lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 12 | # polar: # Replace with a single Polar username 13 | buy_me_a_coffee: "benjaminoakes" 14 | # custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "moment-strftime", 3 | "version": "0.5.0", 4 | "author": "Benjamin Oakes (http://www.benjaminoakes.com/)", 5 | "description": "moment-strftime adds strftime to Moment.js", 6 | "keywords": [ 7 | "moment", 8 | "date", 9 | "strftime" 10 | ], 11 | 12 | "homepage": "https://github.com/benjaminoakes/moment-strftime", 13 | "bugs": "https://github.com/benjaminoakes/moment-strftime/issues", 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/benjaminoakes/moment-strftime.git" 17 | }, 18 | 19 | "license": "MIT", 20 | 21 | "licenses": [ 22 | { 23 | "type": "MIT" 24 | } 25 | ], 26 | 27 | "main": "./lib/moment-strftime.js", 28 | 29 | "engines": { 30 | "node": "*" 31 | }, 32 | 33 | "scripts": { 34 | "test": "jasmine-node spec" 35 | }, 36 | 37 | "dependencies": { 38 | "moment": "^2.11.2" 39 | }, 40 | 41 | "devDependencies": { 42 | "jasmine-node": "1.14.5" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2012 Benjamin Oakes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /lib/moment-strftime.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | var moment, replacements; 3 | 4 | if (typeof require === "function") { 5 | moment = require('moment'); 6 | } else { 7 | moment = this.moment; 8 | } 9 | 10 | replacements = { 11 | 'a': 'ddd', 12 | 'A': 'dddd', 13 | 'b': 'MMM', 14 | 'B': 'MMMM', 15 | 'c': 'lll', 16 | 'd': 'DD', 17 | '-d': 'D', 18 | 'e': 'D', 19 | 'F': 'YYYY-MM-DD', 20 | 'H': 'HH', 21 | '-H': 'H', 22 | 'I': 'hh', 23 | '-I': 'h', 24 | 'j': 'DDDD', 25 | '-j': 'DDD', 26 | 'k': 'H', 27 | 'l': 'h', 28 | 'm': 'MM', 29 | '-m': 'M', 30 | 'M': 'mm', 31 | '-M': 'm', 32 | 'p': 'A', 33 | 'P': 'a', 34 | 'S': 'ss', 35 | '-S': 's', 36 | 'u': 'E', 37 | 'w': 'd', 38 | 'W': 'WW', 39 | 'x': 'll', 40 | 'X': 'LTS', 41 | 'y': 'YY', 42 | 'Y': 'YYYY', 43 | 'z': 'ZZ', 44 | 'Z': 'z', 45 | 'f': 'SSS', 46 | '%': '%' 47 | }; 48 | 49 | moment.fn.strftime = function (format) { 50 | var momentFormat, tokens; 51 | 52 | // Break up format string based on strftime tokens 53 | tokens = format.split(/(%\-?.)/); 54 | momentFormat = tokens.map(function (token) { 55 | // Replace strftime tokens with moment formats 56 | if (token[0] === '%' && replacements.hasOwnProperty(token.substr(1))) { 57 | return replacements[token.substr(1)]; 58 | } 59 | // Escape non-token strings to avoid accidental formatting 60 | return token.length > 0 ? '[' + token + ']' : token; 61 | }).join(''); 62 | 63 | return this.format(momentFormat); 64 | }; 65 | 66 | if (typeof module !== "undefined" && module !== null) { 67 | module.exports = moment; 68 | } else { 69 | this.moment = moment; 70 | } 71 | }).call(this); 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # moment-strftime 2 | 3 | [![Build Status](https://secure.travis-ci.org/benjaminoakes/moment-strftime.png)](http://travis-ci.org/benjaminoakes/moment-strftime) 4 | [![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=benjaminoakes&url=https://github.com/benjaminoakes/moment-strftime&title=moment-strftime&language=en_GB&tags=github&category=software) 5 | 6 | [Moment.js](https://github.com/timrwood/moment) is a great, lightweight date-manipulation library. It also has a very approachable date format syntax that would be familiar to most people who have ever had to fill out a form (e.g., guess what `'YYYY-MM-DD'` means). 7 | 8 | Most programmers however, are familiar with other date formatting syntax. The Unix-style `strftime` is commonly found in many languages' standard libraries. Unfortunately, it is still absent in JavaScript. 9 | 10 | Moment.js helps with a lot of the pain associated with `Date` handling in JavaScript, but it doesn't handle `strftime` (nor will it, [it seems](https://github.com/timrwood/moment/issues/49)). If you are working in a language that does have `strftime`, it seems awkward to have to use another format when using JavaScript (especially if you're trying to keep formats consistent between languages). 11 | 12 | That's unfortunate. There are [too](https://github.com/loopj/commonjs-date-formatting) [many](https://github.com/loopj/commonjs-date-formatting) ([abandoned, buggy](http://hacks.bluesmoon.info/strftime/)) [solutions](https://github.com/zaius/jdate) for date handling in JavaScript. Moment.js has the most steam behind it because of all the other features it has going for it. 13 | 14 | But... if Moment.js just had `strftime` and friends, why would you need anything else? Enter `moment-strftime`. 15 | 16 | ## Installation 17 | 18 | ### Browser 19 | 20 | It's preferred that you use Bower, but you can also [download the raw JavaScript](https://raw.githubusercontent.com/benjaminoakes/moment-strftime/master/lib/moment-strftime.js). 21 | 22 | ### Node.js 23 | 24 | `moment-strftime` is available as a Node.js package. The JavaScript itself should work as a CommonJS module, but it has only been tested in Node.js. 25 | 26 | npm install moment-strftime 27 | 28 | ## Usage 29 | 30 | `moment-strftime` is a tiny plugin for Moment.js that adds a `strftime` method. It's simple: 31 | 32 | ```js 33 | moment().strftime("%m/%d/%y %I:%M %p %Z"); // => '01/17/12 08:54 PM EST' 34 | ``` 35 | 36 | In Node.js: 37 | 38 | ```js 39 | // Gets you everything in Moment.js too 40 | var moment = require('moment-strftime'); 41 | moment().strftime("%m/%d/%y %I:%M %p %Z"); // => '01/17/12 08:54 PM EST' 42 | ``` 43 | 44 | ## Known Issues 45 | 46 | Compatibility: stable Chrome, stable Firefox, stable Safari, IE9+ 47 | 48 | I've only developed `moment-strftime` as far as I need it right now, rather than implementing features I don't need yet. I've noticed that implementing "unused" features often leads to bugs, so the plan is to implement on an as-needed basis. 49 | 50 | If you run into an issue or unimplemented feature that you need, please [open an issue](https://github.com/benjaminoakes/moment-strftime/issues) or (preferably) make a pull request. 51 | 52 | ## Contributing 53 | 54 | To get up and running: 55 | 56 | docker-compose build 57 | docker-compose run test 58 | 59 | ## Resources 60 | 61 | * [cheat strftime](http://errtheblog.com/posts/21-cheat) 62 | * [C strftime](http://pubs.opengroup.org/onlinepubs/007908799/xsh/strftime.html) 63 | 64 | ## License 65 | 66 | MIT (see `LICENSE`) 67 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | moment-strftime (0.5.0) stable; urgency=low 2 | 3 | * Jamie Lubiner: Add support for lowercase meridian indicator (%P for am/pm) 4 | (Closes: #31) 5 | 6 | -- Benjamin Oakes Wed, 29 Aug 2018 11:55:00 +0000 7 | 8 | moment-strftime (0.4.1) stable; urgency=low 9 | 10 | * Reduce maintenance overhead; no functional changes. 11 | 12 | -- Benjamin Oakes Fri, 24 Aug 2018 13:20:00 +0000 13 | 14 | moment-strftime (0.4.0) stable; urgency=low 15 | 16 | * Serhiy Yefremenko: Support for unpadded tokens like %-d (Closes: #30) 17 | 18 | -- Benjamin Oakes Fri, 10 Aug 2018 12:40:00 +0000 19 | 20 | moment-strftime (0.3.2) stable; urgency=low 21 | 22 | * Change minimum version of moment.js to mitigate DoS (Closes: #24, #29) 23 | 24 | -- Benjamin Oakes Fri, 27 Apr 2018 14:30:00 +0000 25 | 26 | moment-strftime (0.3.1) stable; urgency=low 27 | 28 | * Gilmore Davidson: More accurate replacement of strftime tokens 29 | (Closes: #25, #28) 30 | 31 | -- Benjamin Oakes Fri, 27 Apr 2018 14:00:00 +0000 32 | 33 | moment-strftime (0.3.0) stable; urgency=low 34 | 35 | * Sambhav Kothari: Add support for locale-preferred date and time 36 | representation (Closes: #26, #27) 37 | 38 | -- Benjamin Oakes Wed, 21 Mar 2018 14:00:00 +0000 39 | 40 | moment-strftime (0.2.3) stable; urgency=low 41 | 42 | * Ziv Perry: Add support for fractional seconds (Closes: #23) 43 | 44 | -- Benjamin Oakes Mon, 11 Sep 2017 15:30:00 +0000 45 | 46 | moment-strftime (0.2.2) stable; urgency=low 47 | 48 | * Julian Scheid: Relax moment dependency constraint (Closes: #22) 49 | 50 | -- Benjamin Oakes Tue, 14 Jun 2017 20:00:00 +0000 51 | 52 | moment-strftime (0.2.1) stable; urgency=low 53 | 54 | * Jed Fox: Fix webpack critical dependency warning (Closes: #20, #21) 55 | 56 | -- Benjamin Oakes Tue, 28 Feb 2017 10:20:00 -6000 57 | 58 | moment-strftime (0.2.0) stable; urgency=low 59 | 60 | * Start official support for Node 4, 5, 6, and 7. 61 | * Add Docker container for development. 62 | * Remove "build" directory and included minified JavaScript. 63 | * Martin Hradil: Update moment dependency to allow any 2.x version. (Closes: #19) 64 | * Paweł Sierszeń, David Rojo Gonzalez: Fix formatting of normal 65 | non-parameterized text (Closes: #16, #17, #18) 66 | 67 | -- Benjamin Oakes Wed, 26 Oct 2016 10:00:00 -5000 68 | 69 | moment-strftime (0.1.5) stable; urgency=low 70 | 71 | * Martin Hradil: Regenerate build for bower. (Closes: #15) 72 | 73 | -- Benjamin Oakes Wed, 22 Sep 2015 02:42:00 +0000 74 | 75 | moment-strftime (0.1.4) stable; urgency=low 76 | 77 | * Martin Hradil: Add "%k" (hour of day, no leading zero, 24-hour clock), 78 | "%l" (hour of day, no leading zero, 12-hour clock), "%u" (numeric day of week 79 | with Sunday as 7), and "%W" (week number with leading zero) (Closes: #14) 80 | * Martin Hradil: Specify that "%Z" gives "UTC" for a moment.utc time 81 | (Closes: #14) 82 | 83 | -- Benjamin Oakes Wed, 9 Jul 2015 03:20:00 +0000 84 | 85 | moment-strftime (0.1.3) stable; urgency=low 86 | 87 | * Started official support for stable node.js, node.js v0.10.x, node.js 88 | v0.12.x, and stable io.js 89 | * Brandon Pittman: Add "%F" replacement, common shortcut for the date 90 | command in Unix (Closes: #6) 91 | * Pierre Rudloff: Add Bower package (Closes: #7) 92 | * Pierre Rudloff: Update moment dependency (Closes: #11) 93 | * Nicolas: Add "%e" replacement, day of the month without leading zero 94 | (Closes: #12) 95 | 96 | -- Benjamin Oakes Wed, 9 Jul 2015 03:20:00 +0000 97 | 98 | moment-strftime (0.1.2) stable; urgency=low 99 | 100 | * Improve npm user experience. 101 | 102 | -- Benjamin Oakes Tue, 17 Jan 2012 23:39:26 -0500 103 | 104 | moment-strftime (0.1.1) stable; urgency=low 105 | 106 | * Update npm packaging. 107 | 108 | -- Benjamin Oakes Tue, 17 Jan 2012 22:57:15 -0500 109 | 110 | moment-strftime (0.1.0) stable; urgency=low 111 | 112 | * Initial release. 113 | 114 | -- Benjamin Oakes Tue, 17 Jan 2012 22:42:06 -0500 115 | -------------------------------------------------------------------------------- /spec/strftime.spec.js: -------------------------------------------------------------------------------- 1 | var moment = require('../lib/moment-strftime'); 2 | 3 | describe('strftime', function () { 4 | var january17, december2, fraction; 5 | 6 | beforeEach(function () { 7 | moment.locale('en'); // Setting locale to `en` for locale specific tests 8 | january17 = moment('2012-01-17T19:54:20'); // Tuesday 9 | december2 = moment.utc('2029-12-02T01:02:03Z'); // Sunday 10 | fraction = moment('2012-01-17T19:54:20.123'); // Tuesday with microseconds 11 | }); 12 | 13 | describe('given %a', function () { 14 | it('gives the abbreviated weekday name ("Sun")', function () { 15 | expect(january17.strftime('%a')).toEqual('Tue'); 16 | }); 17 | }); 18 | 19 | describe('given %A', function () { 20 | it('gives the full weekday name ("Sunday")', function () { 21 | expect(january17.strftime('%A')).toEqual('Tuesday'); 22 | }); 23 | }); 24 | 25 | describe('given %b', function () { 26 | it('gives the abbreviated month name ("Jan")', function () { 27 | expect(january17.strftime('%b')).toEqual('Jan'); 28 | }); 29 | }); 30 | 31 | describe('given %B', function () { 32 | it('gives the full month name ("January")', function () { 33 | expect(january17.strftime('%B')).toEqual('January'); 34 | }); 35 | }); 36 | 37 | describe('given %c', function () { 38 | it('gives appropriate date and time representation for the locale', function () { 39 | expect(january17.strftime('%c')).toEqual('Jan 17, 2012 7:54 PM'); 40 | }); 41 | }); 42 | 43 | describe('given %d', function () { 44 | it('gives day of the month (01..31)', function () { 45 | expect(january17.strftime('%d')).toEqual('17'); 46 | expect(december2.strftime('%d')).toEqual('02'); 47 | }); 48 | }); 49 | 50 | describe('given %-d', function () { 51 | it('gives day of the month (1..31)', function () { 52 | expect(january17.strftime('%-d')).toEqual('17'); 53 | expect(december2.strftime('%-d')).toEqual('2'); 54 | }); 55 | }); 56 | 57 | describe('given %e', function () { 58 | it('gives day of the month (1..31)', function () { 59 | expect(january17.strftime('%e')).toEqual('17'); 60 | expect(december2.strftime('%e')).toEqual('2'); 61 | }); 62 | }); 63 | 64 | describe('given %H', function () { 65 | it('gives hour of the day, 24-hour clock (00..23)', function () { 66 | expect(january17.strftime('%H')).toEqual('19'); 67 | expect(december2.strftime('%H')).toEqual('01'); 68 | }); 69 | }); 70 | 71 | describe('given %-H', function () { 72 | it('gives hour of the day, 24-hour clock (00..23)', function () { 73 | expect(january17.strftime('%-H')).toEqual('19'); 74 | expect(december2.strftime('%-H')).toEqual('1'); 75 | }); 76 | }); 77 | 78 | describe('given %I', function () { 79 | it('gives hour of the day, 12-hour clock (01..12)', function () { 80 | expect(january17.strftime('%I')).toEqual('07'); 81 | expect(december2.strftime('%I')).toEqual('01'); 82 | }); 83 | }); 84 | 85 | describe('given %-I', function () { 86 | it('gives hour of the day, 12-hour clock (01..12)', function () { 87 | expect(january17.strftime('%-I')).toEqual('7'); 88 | expect(december2.strftime('%-I')).toEqual('1'); 89 | }); 90 | }); 91 | 92 | describe('given %j', function () { 93 | it('gives day of the year (001..366)', function () { 94 | expect(january17.strftime('%j')).toEqual('017'); 95 | }); 96 | }); 97 | 98 | describe('given %-j', function () { 99 | it('gives day of the year (001..366)', function () { 100 | expect(january17.strftime('%-j')).toEqual('17'); 101 | }); 102 | }); 103 | 104 | describe('given %k', function () { 105 | it('gives hour of the day, 24-hour clock (0..23)', function () { 106 | expect(january17.strftime('%k')).toEqual('19'); 107 | expect(december2.strftime('%k')).toEqual('1'); 108 | }); 109 | }); 110 | 111 | describe('given %l', function () { 112 | it('gives hour of the day, 12-hour clock (1..12)', function () { 113 | expect(january17.strftime('%l')).toEqual('7'); 114 | expect(december2.strftime('%l')).toEqual('1'); 115 | }); 116 | }); 117 | 118 | describe('given %m', function () { 119 | it('gives month of the year (01..12)', function () { 120 | expect(january17.strftime('%m')).toEqual('01'); 121 | }); 122 | }); 123 | 124 | describe('given %-m', function () { 125 | it('gives month of the year (01..12)', function () { 126 | expect(january17.strftime('%-m')).toEqual('1'); 127 | }); 128 | }); 129 | 130 | describe('given %M', function () { 131 | it('gives minute of the hour (00..59)', function () { 132 | expect(december2.strftime('%M')).toEqual('02'); 133 | }); 134 | }); 135 | 136 | describe('given %-M', function () { 137 | it('gives minute of the hour (00..59)', function () { 138 | expect(december2.strftime('%-M')).toEqual('2'); 139 | }); 140 | }); 141 | 142 | describe('given %p', function () { 143 | it('gives meridian indicator ("AM" or "PM")', function () { 144 | expect(january17.strftime('%p')).toEqual('PM'); 145 | }); 146 | }); 147 | 148 | describe('given %P', function () { 149 | it('gives meridian indicator ("am" or "pm")', function () { 150 | expect(january17.strftime('%P')).toEqual('pm'); 151 | }); 152 | }); 153 | 154 | describe('given %S', function () { 155 | it('gives second of the minute (00..60)', function () { 156 | expect(december2.strftime('%S')).toEqual('03'); 157 | }); 158 | }); 159 | 160 | describe('given %-S', function () { 161 | it('gives second of the minute (00..60)', function () { 162 | expect(december2.strftime('%-S')).toEqual('3'); 163 | }); 164 | }); 165 | 166 | describe('given %u', function () { 167 | it('gives day of the week (Sunday is 7, 1..7)', function () { 168 | expect(january17.strftime('%u')).toEqual('2'); 169 | expect(december2.strftime('%u')).toEqual('7'); 170 | }); 171 | }); 172 | 173 | describe('given %w', function () { 174 | it('gives day of the week (Sunday is 0, 0..6)', function () { 175 | expect(january17.strftime('%w')).toEqual('2'); 176 | expect(december2.strftime('%w')).toEqual('0'); 177 | }); 178 | }); 179 | 180 | describe('given %W', function () { 181 | it('gives the week number (00-53)', function () { 182 | expect(january17.strftime('%W')).toEqual('03'); 183 | expect(december2.strftime('%W')).toEqual('48'); 184 | }); 185 | }); 186 | 187 | describe('given %x', function () { 188 | it('gives appropriate date representation for the locale', function () { 189 | expect(january17.strftime('%x')).toEqual('Jan 17, 2012'); 190 | }); 191 | }); 192 | 193 | describe('given %X', function () { 194 | it('gives appropriate time representation for the locale', function () { 195 | expect(january17.strftime('%X')).toEqual('7:54:20 PM'); 196 | }); 197 | }); 198 | 199 | describe('given %y', function () { 200 | it('gives year without a century (00..99)', function () { 201 | expect(january17.strftime('%y')).toEqual('12'); 202 | }); 203 | }); 204 | 205 | describe('given %Y', function () { 206 | it('gives year with century', function () { 207 | expect(january17.strftime('%Y')).toEqual('2012'); 208 | }); 209 | }); 210 | 211 | describe('given %Z', function () { 212 | it('gives an empty string because it is deprecated', function () { 213 | expect(january17.strftime('%Z')).toEqual(''); 214 | }); 215 | 216 | it('gives "UTC" for a moment.utc object', function () { 217 | expect(december2.strftime('%Z')).toEqual('UTC'); 218 | }); 219 | }); 220 | 221 | describe('given %z', function () { 222 | it('gives the UTC offset', function () { 223 | expect(january17.strftime('%z')).toMatch(/[\-+]\d{4}/); 224 | }); 225 | }); 226 | 227 | describe('given %f', function () { 228 | it('gives the micro-seconds', function () { 229 | expect(fraction.strftime('%f')).toEqual('123'); 230 | }); 231 | }); 232 | 233 | describe('given %%', function () { 234 | it('gives "%" character', function () { 235 | expect(january17.strftime('%%')).toEqual('%'); 236 | }); 237 | }); 238 | 239 | describe('given an unknown token', function() { 240 | it('gives the same token unreplaced', function() { 241 | expect(january17.strftime('%Q')).toEqual('%Q'); 242 | }); 243 | }); 244 | 245 | describe('given %d of month %m', function () { 246 | it('gives "%d of month %m"', function () { 247 | expect(january17.strftime('%d of month %m')).toEqual('17 of month 01'); 248 | }); 249 | }); 250 | 251 | describe('given any non-parametrized text', function () { 252 | it('gives the same text', function () { 253 | expect(january17.strftime('hello world')).toEqual('hello world'); 254 | }); 255 | }); 256 | 257 | describe('given a compound format', function() { 258 | it('formats correctly with distinct tokens', function () { 259 | expect(january17.strftime('%m/%d/%y %I:%M %p')).toEqual('01/17/12 07:54 PM'); 260 | }); 261 | 262 | it('formats correctly with tokens surrounding other letters', function() { 263 | expect(january17.strftime('%Y-%m-%dT%H:%M')).toEqual('2012-01-17T19:54'); 264 | }); 265 | 266 | it('formats correctly with tokens within words', function() { 267 | expect(january17.strftime('a code is month%m%Yyear')).toEqual('a code is month012012year'); 268 | }); 269 | }); 270 | }); 271 | --------------------------------------------------------------------------------