Convert a string date format from 17-Nov-2011 to 11/17/11
Use the built-in Time.parse
and Time#strftime
functions.
require 'time'
time = Time.parse("17-Nov-2011")
time.strftime("%m/%d/%y")
# => "11/17/11"
How to convert %Y%m%d%H%m%s to YYYY-mm-dd HH:mm:ss?
require 'date'
date = '20170802173300'
formatted_date = DateTime.parse(date).to_s
# => "2017-08-02T17:33:00+00:00"
# and to be more explicit:
really_formated_date = DateTime.parse(date).strftime('%Y-%m-%d %H:%M:%S %Z')
# => "2017-08-02 17:33:00 +00:00"
How do you format the day of the month to say 11th , 21st or 23rd (ordinal indicator)?
// https://github.com/google/guava
import static com.google.common.base.Preconditions.*;
String getDayOfMonthSuffix(final int n) {
checkArgument(n >= 1 && n <= 31, "illegal day of month: " + n);
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
The table from @kaliatech is nice, but since the same information is repeated, it opens the chance for a bug. Such a bug actually exists in the table for 7tn
, 17tn
, and 27tn
(this bug might get fixed as time goes on because of the fluid nature of StackOverflow, so check the version history on the answer to see the error).
Rails - convert date string into javascript date object for highcharts
This probably isn't the best way to do this, but I've been doing:
data: <%= @orders.map{ |f| [f.date.to_datetime.to_i, pdd.field_5x5 ] }.inspect %>,
...which expresses the date in seconds.
Format date in MySQL to return MonthName, Year
Try this
SELECT DATE_FORMAT(`date`,'%M %Y') AS showdate FROM table
How can I convert Double Date to std::string
Look into Boost.Lexical_cast for conversion to and from string.
You will have to write a converter function that unpacks the DATE type into a (date, time) tuple so that lexical_cast won't convert from double.
According to the doc, it looks like the date type is (day - 1899-12-30) . (time - 0).
Using boost::date_time, you can create a time object with date-time (1899-12-30,0), then increment days(abs(DATE))
and hours((DATE - abs(DATE)) * 24)
.
Adding minutes to date time in PHP
$minutes_to_add = 5;
$time = new DateTime('2011-11-17 05:05');
$time->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$stamp = $time->format('Y-m-d H:i');
The ISO 8601 standard for duration is a string in the form of P{y}Y{m1}M{d}DT{h}H{m2}M{s}S
where the {*}
parts are replaced by a number value indicating how long the duration is.
For example, P1Y2DT5S
means 1 year, 2 days, and 5 seconds.
In the example above, we are providing PT5M
(or 5 minutes) to the DateInterval
constructor.
date format issue with asp.net and sql server
If your data type on the database side is either datetime
or date
(on sql server 2008), it shouldn't matter whether you insert the date as '10 june 2011'
or as '6/10/2011'
or as '2011-06-10'
. If you see the data actually being displayed as '10 June 2011'
that's more likely because your data type is varchar
or nvarchar
. If that's the case and the field is only meant to hold dates, I would advise you to change the data type to be actually a datetime
or date
.
Display dates in Arabic
AFAIK setlocale
won't actually do any language translation for you but rather affects things like the formatting and comparator functionality. If you want localisation then you could try using IntlDateFormatter which may give you what you need.
Updated: You could also try Zend_Date as suggested in this question if PHP 5.3 isn't an option for you.
Related Topics
Ruby Class Instance Variables and Inheritance
Rails Before_Filter for Specific Actions in Controller
Accepts_Nested_Attributes_For Rails 4 Is Not Deleting
How to Start the Ruby Debugger on Exception
Simple Ruby Input Validation Library
Mongoid: Find Through Array of Ids
Ruby Way to Group Anagrams in String Array
Convert String with Comma to Integer
How to Organize Minitest/Unit Tests
How to View a Sample of the Call Stack in Ruby
Check If String Contains Any Substring in an Array in Ruby
Is Time.Zone.Now.To_Date Equivalent to Date.Today
How to Trace and Check Dependencies in Bundled Ruby Gems
How to Convert String to Bytes in Ruby