Anna
Anna

Reputation: 203

show only year-month-day from a DateTime value

I have this code:

order.bookingDate()

and i get this: 2013-11-22T10:15:00.000-08:00

But I just want to show this: 2013-11-22 in the webpage, and if it's possible in this format: 22-11-2013, any idea? Thanks!

Upvotes: 1

Views: 10432

Answers (6)

Dugini Vijay
Dugini Vijay

Reputation: 923

from datetime import datetime - `datetime.now()` Expected Output : 2018-11-15T11:14:00.000-08:00 Need only date: - `datetime.date(datetime.now())`

If you need to strip the date or year or time and arrange them as per your requirements. Play around adjusting the values in which ever place you want. you can do the following:

- `datetime.now().strftime("%Y-%m-%d-%H-%M")` Expected Output: YYYY-MM-DD-HH-MM '2018-11-15-11-14'

Upvotes: 0

elm
elm

Reputation: 20415

Using Joda,

import org.joda.time.DateTime
val dt = new DateTime("2013-11-22T10:15:00.000-08:00")

Then

dt.toYearMonthDay.toString
String = 2013-11-22

and

dt2.getDayOfMonth + "-" + dt2.getMonthOfYear + "-" + dt2.getYear
String = 22-11-2013

Upvotes: 0

sms247
sms247

Reputation: 4504

dateTime.ToString("yyyy-MM-dd");

If you have date time in string then first convert it to datetime as follows

DateTime dateTime;
dateTime = new DateTime();
dateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

Formats for Date

d - Numeric day of the month without a leading zero.
dd - Numeric day of the month with a leading zero.
ddd - Abbreviated name of the day of the week.
dddd - Full name of the day of the week.

M - Numeric month with no leading zero.
MM - Numeric month with a leading zero.
MMM - Abbreviated name of month.
MMMM - Full month name.

y - Year with out century and leading zero.
yy - Year with out century, with leading zero.
yyyy - Year with century.

Formats for Time

h - 12 Hour clock, no leading zero.
hh - 12 Hour clock with leading zero.
H - 24 Hour clock, no leading zero.
HH - 24 Hour clock with leading zero.

m - Minutes with no leading zero.
mm - Minutes with leading zero.

s - Seconds with no leading zero.
ss - Seconds with leading zero.

t - AM/PM but only the first letter. 
tt - AM/PM ( a.m. / p.m.)

zz - Time zone off set with +/-

Upvotes: 3

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

You Can Simply use your format in ToString() method:

order.bookingDate.ToString("yyyy-MM-dd")

Upvotes: 1

Vladimir Matveev
Vladimir Matveev

Reputation: 128011

Use DateTimeFormat object:

val fmt = DateTimeFormat.forPattern("yyyy-MM-dd")
fmt.print(order.bookingDate())

You can save DateTimeFormat object in object field as a global constant, it is thread-safe. Or you can use toString() method directly:

order.bookingDate().toString("yyyy-MM-dd")

See DateTimeFormat API documentation (link above) for more information on possible date/time patterns. For example, this format: 22-11-2013 is easily achieved by reversing formatting pattern in previous example:

order.bookingDate().toString("dd-MM-yyyy")

Upvotes: 1

Shadowlands
Shadowlands

Reputation: 15074

You can use a regular expression to pattern match and get what you want. Eg:

val rawDate = "2013-11-22T10:15:00.000-08:00" //order.bookingDate() or order.bookingDate().toString

val YMD = """(\d\d\d\d)-(\d\d)-(\d\d).*""".r // Three capture groups of 4,2, and 2 digits, followed by stuff we don't care about ('.*')

val YMD(y, m, d) = rawDate // unapply to store the captured groups into new vals y, m, d

val outStr = s"$d-$m-$y" // outStr = "22-11-2013"

Upvotes: 1

Related Questions