How to print name of the day of the week?
You are using the wrong date format. The correct format is "yyyy-M-d"
. Besides that you can use Calendar property weekdaySymbols which returns the weekday localized.
func getDayOfWeek(_ date: String) -> String? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-M-d"
formatter.locale = Locale(identifier: "en_US_POSIX")
guard let todayDate = formatter.date(from: date) else { return nil }
let weekday = Calendar(identifier: .gregorian).component(.weekday, from: todayDate)
return Calendar.current.weekdaySymbols[weekday-1] // "Monday"
}
Another option is to use DateFormatter and set your dateFormat to "cccc"
as you can see in this answer:
extension Formatter {
static let weekdayName: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "cccc"
return formatter
}()
static let customDate: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-M-d"
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()
}
extension Date {
var weekdayName: String { Formatter.weekdayName.string(from: self) }
}
Using the extension above your function would look like this:
func getDayOfWeek(_ date: String) -> String? { Formatter.customDate.date(from: date)?.weekdayName }
Playground testing:
getDayOfWeek("2018-3-5") // Monday
Date().weekdayName // Thursday
Python print day name of week from number
In python, for statements like if, for, etc. You have to add :
at the end of it.
And for comparing (for equal) you have to use ==
and not =
num = 4
if(num == 1):
print('Monday')
elif num == 2:
print('Tuesday')
.....
You can compare without parenthesis and it will work too.
How do I get the day of week given a date?
Use weekday()
:
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2012, 3, 23, 23, 24, 55, 173504)
>>> datetime.datetime.today().weekday()
4
From the documentation:
Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
How get the name of the days of the week in Dart
Use 'EEEE' as a date pattern
DateFormat('EEEE').format(date); /// e.g Thursday
Don't forget to import
import 'package:intl/intl.dart';
Check this for more info : https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html
How to determine day of week by passing specific date?
Yes. Depending on your exact case:
You can use
java.util.Calendar
:Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);if you need the output to be
Tue
rather than 3 (Days of week are indexed starting at 1 for Sunday, see Calendar.SUNDAY), instead of going through a calendar, just reformat the string:new SimpleDateFormat("EE").format(date)
(EE
meaning "day of week, short version")if you have your input as string, rather than
Date
, you should useSimpleDateFormat
to parse it:new SimpleDateFormat("dd/M/yyyy").parse(dateString)
you can use joda-time's
DateTime
and calldateTime.dayOfWeek()
and/orDateTimeFormat
.edit: since Java 8 you can now use java.time package instead of joda-time
How to get day name from datetime
import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))
See the Python docs for datetime.now, datetime.strftime and more on strftime.
How can I output the day of week with embedded ruby?
Say i have date = Time.now.to_date then date.strftime("%A") will print name for the day of the week and to have just the number for the day of the week write date.wday.
Copy from here
So Time.now.strftime("%A")
and in your case
<h6>
<%= Time.now.strftime("%A, %B %d. %Y") %>
</h6>
Easiest to test this is the rails c
. To remove the leading zero on the %d
use %-d
instead.
Get Day name from Weekday int
It is more Pythonic to use the calendar module:
>>> import calendar
>>> list(calendar.day_name)
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
Or, you can use common day name abbreviations:
>>> list(calendar.day_abbr)
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
Then index as you wish:
>>> calendar.day_name[1]
'Tuesday'
(If Monday is not the first day of the week, use setfirstweekday to change it)
Using the calendar module has the advantage of being location aware:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
'de_DE'
>>> list(calendar.day_name)
['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag']
Related Topics
iOS How to Get a List of Already Purchased Products
Set Custom Font for UItableview Swipe Action (Uicontextualaction)
Ld: Library Not Found for -Ldoubleconversion React Native 0.59
Screenshot on Swift Programmatically
How to Draw a Uilabel with a Different Blend Mode in Draw(_ Rect: Cgrect) in Swift
Fixing Low Fps in Swift Playground
Call Extensiondelegate to Create/Refresh Data for Complication
Determine The Maximum Number of Characters a UIlabel Can Take
How to Access All Images in a .Xcassets at Once
Conversion from String to Date in Swift Returns Nil
Terminating App Due to Uncaught Exception 'Nsinvalidargumentexception' - iOS Google Sign In
Exceeding Max Text("") Concatenation Length - Swiftui -
Play Keyboard Click Sound in a Collection View Controller
How to Install Self-Signed Certificates in iOS 11
How to Get a Low Res Image, or Thumbnail from the Alassetrepresentation in Swift
How to Add Action to Uialertview in Swift (iOS 7)