Date Formatter isn't showing correct day of the month when using format “DD”How do I get the current Date in short format in SwiftGet Last Day of the Month in PythonHow to subtract a day from a date?Add days to JavaScript DateWhere can I find documentation on formatting a date in JavaScript?jQuery UI DatePicker - Change Date FormatYYYY-MM-DD format date in shell scriptCalculate difference between two dates (number of days)?Get month name from DateConvert date format yyyy-mm-dd => dd-mm-yyyyHow to format a JavaScript date
Why are there yellow dot stickers on the front doors of businesses in Russia?
split inside flalign
How do people drown while wearing a life jacket?
Is a text with orthographic or grammatic mistakes in a language X still a text in that language X?
What printing process is this?
Why does capacitance not depend on the material of the plates?
Repeated! Factorials!
If someone else uploads my GPL'd code to Github without my permission, is that a copyright violation?
C# TCP server/client class
How do I know when and if a character requires a backstory?
Why do my fried eggs start browning very fast?
Awk to get all my regular users in shadow
Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?
How do I show and not tell a backstory?
Why is it to say 'paucis post diebus'?
What does "autolyco-sentimental" mean?
How to prevent Deadlock on SELECT queries?
ZFS on Linux: Which mountpoint option when mounting manually per script?
Generate random number in Unity without class ambiguity
How can I perform a deterministic physics simulation?
RS-485 network with multiple masters
In MTG, was there ever a five-color deck that worked well?
Did Logical Positivism fail because it simply denied human emotion?
Make lens aperture in Tikz
Date Formatter isn't showing correct day of the month when using format “DD”
How do I get the current Date in short format in SwiftGet Last Day of the Month in PythonHow to subtract a day from a date?Add days to JavaScript DateWhere can I find documentation on formatting a date in JavaScript?jQuery UI DatePicker - Change Date FormatYYYY-MM-DD format date in shell scriptCalculate difference between two dates (number of days)?Get month name from DateConvert date format yyyy-mm-dd => dd-mm-yyyyHow to format a JavaScript date
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to show today's date as March 26 but it is showing as "March 85" when I use this code.
let formatter = DateFormatter()
formatter.dateFormat = "MMMM DD"
let defaultTimeZoneStr = formatter.string(from: Date())
swift date time formatter
add a comment |
I am trying to show today's date as March 26 but it is showing as "March 85" when I use this code.
let formatter = DateFormatter()
formatter.dateFormat = "MMMM DD"
let defaultTimeZoneStr = formatter.string(from: Date())
swift date time formatter
add a comment |
I am trying to show today's date as March 26 but it is showing as "March 85" when I use this code.
let formatter = DateFormatter()
formatter.dateFormat = "MMMM DD"
let defaultTimeZoneStr = formatter.string(from: Date())
swift date time formatter
I am trying to show today's date as March 26 but it is showing as "March 85" when I use this code.
let formatter = DateFormatter()
formatter.dateFormat = "MMMM DD"
let defaultTimeZoneStr = formatter.string(from: Date())
swift date time formatter
swift date time formatter
edited Mar 27 at 4:09
Cœur
21.6k10 gold badges126 silver badges173 bronze badges
21.6k10 gold badges126 silver badges173 bronze badges
asked Mar 27 at 2:38
AzaAza
187 bronze badges
187 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The problem is that you are using the wrong date format. D
is for "day of year". The correct symbol for "day of month" is lowercased d
Thats why you are getting 85
instead of 26
.
Another point you should consider is to set your locale fixed to "en_US_POSIX"
if you don't want your date string to reflect the users settings and locale.
Note that you should use Swift native type Date
instead of NSDate
.
If your intent is to display it respecting the user locale and settings you should use date formatter dateStyle
(short, medium, long or full) How do I get the current Date in short format in Swift
If you need a localized date format limited to
month and day only, you can use DateFormatter
method dateFormat from template:
class func dateFormat(fromTemplate tmplate: String, options opts: Int, locale: Locale?) -> String?
let df = DateFormatter()
df.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMMdd", options: 0, locale: .current)
df.string(from: Date()) // "March 27"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368964%2fdate-formatter-isnt-showing-correct-day-of-the-month-when-using-format-dd%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that you are using the wrong date format. D
is for "day of year". The correct symbol for "day of month" is lowercased d
Thats why you are getting 85
instead of 26
.
Another point you should consider is to set your locale fixed to "en_US_POSIX"
if you don't want your date string to reflect the users settings and locale.
Note that you should use Swift native type Date
instead of NSDate
.
If your intent is to display it respecting the user locale and settings you should use date formatter dateStyle
(short, medium, long or full) How do I get the current Date in short format in Swift
If you need a localized date format limited to
month and day only, you can use DateFormatter
method dateFormat from template:
class func dateFormat(fromTemplate tmplate: String, options opts: Int, locale: Locale?) -> String?
let df = DateFormatter()
df.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMMdd", options: 0, locale: .current)
df.string(from: Date()) // "March 27"
add a comment |
The problem is that you are using the wrong date format. D
is for "day of year". The correct symbol for "day of month" is lowercased d
Thats why you are getting 85
instead of 26
.
Another point you should consider is to set your locale fixed to "en_US_POSIX"
if you don't want your date string to reflect the users settings and locale.
Note that you should use Swift native type Date
instead of NSDate
.
If your intent is to display it respecting the user locale and settings you should use date formatter dateStyle
(short, medium, long or full) How do I get the current Date in short format in Swift
If you need a localized date format limited to
month and day only, you can use DateFormatter
method dateFormat from template:
class func dateFormat(fromTemplate tmplate: String, options opts: Int, locale: Locale?) -> String?
let df = DateFormatter()
df.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMMdd", options: 0, locale: .current)
df.string(from: Date()) // "March 27"
add a comment |
The problem is that you are using the wrong date format. D
is for "day of year". The correct symbol for "day of month" is lowercased d
Thats why you are getting 85
instead of 26
.
Another point you should consider is to set your locale fixed to "en_US_POSIX"
if you don't want your date string to reflect the users settings and locale.
Note that you should use Swift native type Date
instead of NSDate
.
If your intent is to display it respecting the user locale and settings you should use date formatter dateStyle
(short, medium, long or full) How do I get the current Date in short format in Swift
If you need a localized date format limited to
month and day only, you can use DateFormatter
method dateFormat from template:
class func dateFormat(fromTemplate tmplate: String, options opts: Int, locale: Locale?) -> String?
let df = DateFormatter()
df.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMMdd", options: 0, locale: .current)
df.string(from: Date()) // "March 27"
The problem is that you are using the wrong date format. D
is for "day of year". The correct symbol for "day of month" is lowercased d
Thats why you are getting 85
instead of 26
.
Another point you should consider is to set your locale fixed to "en_US_POSIX"
if you don't want your date string to reflect the users settings and locale.
Note that you should use Swift native type Date
instead of NSDate
.
If your intent is to display it respecting the user locale and settings you should use date formatter dateStyle
(short, medium, long or full) How do I get the current Date in short format in Swift
If you need a localized date format limited to
month and day only, you can use DateFormatter
method dateFormat from template:
class func dateFormat(fromTemplate tmplate: String, options opts: Int, locale: Locale?) -> String?
let df = DateFormatter()
df.dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMMdd", options: 0, locale: .current)
df.string(from: Date()) // "March 27"
edited Mar 27 at 11:17
answered Mar 27 at 3:34
Leo DabusLeo Dabus
147k35 gold badges316 silver badges387 bronze badges
147k35 gold badges316 silver badges387 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368964%2fdate-formatter-isnt-showing-correct-day-of-the-month-when-using-format-dd%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown