swift 3 how to get date for tomorrow and yesterday ( take care special case ) new month or new year [duplicate]

You should use Calendar method date(byAdding component:) to do your calendrical calculations using noon time. Doing so you don’t need to worry about those special cases: Swift 3 or Later extension Date { static var yesterday: Date { return Date().dayBefore } static var tomorrow: Date { return Date().dayAfter } var dayBefore: Date { return Calendar.current.date(byAdding: … Read more

Moment.js get day name from date

With moment you can parse the date string you have: var dt = moment(myDate.date, “YYYY-MM-DD HH:mm:ss”) That’s for UTC, you’ll have to convert the time zone from that point if you so desire. Then you can get the day of the week: dt.format(‘dddd’); And you can get the 3 letter format weekday by dt.format(‘dddd’).substring(0,3)

Formatting a calendar date

Calendar calendar = Calendar.getInstance(); SimpleDateFormat format = new SimpleDateFormat(“EEEE, MMMM d, yyyy ‘at’ h:mm a”); System.out.println(format.format(calendar.getTime())); Running the above code outputs the current time (e.g., Saturday, May 26, 2012 at 11:03 PM). See the Android documentation for SimpleDateFormat for more information. The format specification of SimpleDateFormat is similar to that of PHP’s date function: echo … Read more

Why does Java’s Date.getYear() return 111 instead of 2011?

Those methods have been deprecated. Instead, use the Calendar class. import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; public final class DateParseDemo { public static void main(String[] args){ final DateFormat df = new SimpleDateFormat(“MM/dd/yyyy”); final Calendar c = Calendar.getInstance(); try { c.setTime(df.parse(“04/12/2011”)); System.out.println(“Year = ” + c.get(Calendar.YEAR)); System.out.println(“Month = ” + (c.get(Calendar.MONTH))); System.out.println(“Day = ” … Read more

Getting current datetime using Calendar.getInstance() vs new GregorianCalendar()

Looking in the source of Calendar.getInstance(): private static Calendar createCalendar(TimeZone zone, Locale aLocale) { // If the specified locale is a Thai locale, returns a BuddhistCalendar // instance. if (“th”.equals(aLocale.getLanguage()) && (“TH”.equals(aLocale.getCountry()))) { return new sun.util.BuddhistCalendar(zone, aLocale); } else if (“JP”.equals(aLocale.getVariant()) && “JP”.equals(aLocale.getCountry()) && “ja”.equals(aLocale.getLanguage())) { return new JapaneseImperialCalendar(zone, aLocale); } // else create the … Read more

First day of next month with java Joda-Time

LocalDate today = new LocalDate(); LocalDate d1 = today.plusMonths(1).withDayOfMonth(1); A little easier and cleaner, isn’t it? 🙂 Update: If you want to return a date: return new Date(d1.toDateTimeAtStartOfDay().getMillis()); but I strongly advise you to avoid mixing pure DATE types (i.e. a day in the calendar, without time information) with DATETIME types, specially with a “physical” … Read more

NSCalendar first day of week

Edit: This does not check the edge case where the beginning of the week starts in the prior month. Some updated code to cover this: https://stackoverflow.com/a/14688780/308315 In case anyone is still paying attention to this, you need to use ordinalityOfUnit:inUnit:forDate: and set firstWeekday to 2. (1 == Sunday and 7 == Saturday) Here’s the code: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)