Date format: Difference between revisions

Add ed example
(Add ed example)
 
(16 intermediate revisions by 10 users not shown)
Line 1,705:
me_msg()_now()_format(yyyy-mm-dd);
me_msg()_now()_format(eeee, mmmm dd, yyyy);</syntaxhighlight>
 
=={{header|Ed}}==
 
Using <tt>date</tt> is cheating, but ed has no built-in date fetching/formatting. Most other languages use strftime, which is basically the same thing, anyway.
 
<syntaxhighlight lang="sed">
H
!date +"\%Y-\%m-\%d"
!date +"\%A, \%B \%d, \%Y"
Q
</syntaxhighlight>
 
=={{header|EGL}}==
Line 1,890 ⟶ 1,901:
Forth is less of a language and more of an extensible toolkit of simple routines. This version attempts to demonstrate using the simple routines to extend Forth. Then using the language extensions and the power of concatenative language to solve the problem. This solution could create numerous date formats as one line definitions now that we have our "date" words defined. Typically these extensions would be saved as a library file.
 
<LANGsyntaxhighlight FORTHlang="forth">\ Build up a "language" for date formatting
 
\ utility words
Line 1,950 ⟶ 1,961:
\ Rosetta Date Format 2
: LONG.DATE ( d m y -- )
3DUP CDAY DOW ]DAY$. ',' -ROT ]MONTH$. SPACE ##. ',' ####. ;</LANGsyntaxhighlight>
 
 
 
Test at the Forth Console
<LANGsyntaxhighlight FORTHlang="forth"> 5 7 2018 Y-M-D. 2018-07-05 ok
ok
5 7 2018 LONG.DATE Thursday, July 05, 2018 ok
</syntaxhighlight>
</LANG>
 
=={{header|Fortran}}==
Line 2,181 ⟶ 2,193:
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public static void main(String[] args) {
long millis = System.currentTimeMillis();
System.out.printf("%tF%n", millis);
System.out.printf("%tA, %1$tB %1$td, %1$tY%n", millis);
}
</syntaxhighlight>
<pre>
2023-05-10
Wednesday, May 10, 2023
</pre>
<br />
An alternate demonstration
<syntaxhighlight lang="java">
import java.util.Calendar;
Line 2,300 ⟶ 2,325:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln string(dt//, "2006-01-02")
{{works with|langur|0.10.1}}
writeln string(dt//, "Monday, January 2, 2006")
<syntaxhighlight lang="langur">var .now = dt//
</syntaxhighlight>
var .format1 = "2006-01-02"
var .format2 = "Monday, January 2, 2006"
writeln $"\.now:dt.format1;"
writeln $"\.now:dt.format2;"</syntaxhighlight>
 
<syntaxhighlight lang="langur">
{{works with|langur|0.9.3}}
writeln "{{dt//:dt(2006-01-02)}}"
<syntaxhighlight lang="langur">var .now = dt//
writeln $"\.now{{dt//:dt(Monday, January 2, 2006-01-02);}}"
writeln $"\.now:dt(Monday, January 2, 2006);"</syntaxhighlight>
 
{{works with|langur|0.9}}
<syntaxhighlight lang="langur">writeln toString dt//, "2006-01-02"
writeln toString dt//, "Monday, January 2, 2006"</syntaxhighlight>
 
{{out}}
<pre>2020-03-26
Thursday, March 26, 2020</pre>
 
Langur is using the Go time package, and using its formatting method for output. It expects the following parts in a format string.
 
<pre>year: 2006 or 06
month: 01 or 1
month name: Jan or January
month day: 02 or _2 or 2
weekday name: Mon or Monday
hour: 03 or 3 or 15
minute: 04 or 4
second: 05 or 5
AM/PM: PM or pm
time zone offset: -07:00 or -0700 or -07
time zone name: MST</pre>
 
=={{header|Lasso}}==
Line 2,428 ⟶ 2,432:
2019-04-02
Tuesday, April 02, 2019
</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">import "dateTime"
 
print dateTime.now("yyyy-MM-dd")
print dateTime.now("dddd, MMMM d, yyyy")</syntaxhighlight>
 
{{out}}
<pre>2023-12-28
Thursday, December 28, 2023
</pre>
 
Line 2,775 ⟶ 2,790:
<pre>2010-07-30
Friday, July 30, 2010</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
begin
var today := DateTime.Now;
Println(today.ToString('yyyy-MM-dd'));
Println($'{today:D}');
end.
</syntaxhighlight>
{{out}}
<pre>
2024-06-01
Saturday, June 1, 2024
</pre>
 
=={{header|Perl}}==
Line 3,200 ⟶ 3,229:
2020-07-19
Sunday, July 19, 2020
</pre>
 
=={{header|RPL}}==
RPL can return the date as a floating point number (format dd.mmyyyy) and can convert it (along with the time) as a string (format "DAY dd.mm.yy hh.mm.ss")
To solve the task, some formatting is then needed.
{{works with|HP|48}}
≪ DATE DUP 1000000 * 10000 MOD "-" +
OVER FP 100 * IP + "-" +
SWAP IP +
≫ '<span style="color:blue">DTSHORT</span>' STO
≪ { "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday" }
{ "MON" "TUE" "WED" "THU" "FRI" "SAT" "SUN" }
DATE TIME TSTR 1 3 SUB POS GET ", " +
{ "January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"}
DATE FP 100 * IP GET + " " +
OVER IP + ", " +
DATE 1000000 * 10000 MOD +
≫ '<span style="color:blue">DTLONG</span>' STO
{{out}}
<pre>
2: "2023-8-17"
1: "Thursday, August 17, 2023"
</pre>
 
Line 3,238 ⟶ 3,290:
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">val now=new Date()
import java.util.Date
 
val now=new Date()
println("%tF".format(now))
println("%1$tA, %1$tB %1$td, %1$tY".format(now))</syntaxhighlight>
 
{{out}}
<pre>
2023-04-17
Monday, April 17, 2023
</pre>
 
=={{header|Scheme}}==
Line 3,325 ⟶ 3,386:
Donnerstag, Dezember 17, 2020
Vendredi, Décembre 17, 2020</pre>
 
=={{header|SparForte}}==
As a structured script. Does not use l10n package.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "dateformat")
@( description, "Display the current date in the formats of '2007-11-10' " )
@( description, "and 'Sunday, November 10, 2007'." )
@( see_also, "http://rosettacode.org/wiki/Date_format" )
@( "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure dateformat is
function Month_Image (Month : calendar.month_number) return string is
begin
case Month is
when 1 => return "January";
when 2 => return "February";
when 3 => return "March";
when 4 => return "April";
when 5 => return "May";
when 6 => return "June";
when 7 => return "July";
when 8 => return "August";
when 9 => return "September";
when 10 => return "October";
when 11 => return "November";
when others => return "December";
end case;
end Month_Image;
function Day_Image (Day : integer) return string is
begin
case Day is
when 0 => return "Monday";
when 1 => return "Tuesday";
when 2 => return "Wednesday";
when 3 => return "Thursday";
when 4 => return "Friday";
when 5 => return "Saturday";
when others => return "Sunday";
end case;
end Day_Image;
Today : constant calendar.time := calendar.clock;
begin
--put_line(
--Put_Line (Image (Today) (1..10));
 
put( calendar.year( Today ), "9999" ) @( "-" )
@( calendar.month( Today ), "99" ) @( "-" )
@( calendar.day( Today ), "99" );
new_line;
 
put_line(
Day_Image( calendar.day_of_week( Today ) ) & ", " &
Month_Image( calendar.month( Today ) ) &
strings.image( calendar.day( Today ) ) & "," &
strings.image( calendar.year( Today ) ) );
end dateformat;</syntaxhighlight>
 
=={{header|SQL}}==
Line 3,607 ⟶ 3,727:
<syntaxhighlight lang="vedit">RI(1) IT(", ") RI(2) IT(" ") NI(#1, LEFT+NOCR) IT(",") NI(#3)</syntaxhighlight>
 
=={{header|VlangVim Script}}==
See `:help strftime` - this function uses the C strftime() arguments' format.
<syntaxhighlight lang="vim">
echo strftime("%Y-%m-%d")
echo strftime("%A, %B %d, %Y")
</syntaxhighlight>
 
=={{header|V (Vlang)}}==
 
<syntaxhighlight lang="v (vlang)">import time
 
fn main() {
Line 3,624 ⟶ 3,751:
{{libheader|Wren-date}}
Unless it is embedded in a host application, Wren currently has no direct way to obtain the current date. We therefore assume this is passed in by the user as a command line parameter.
<syntaxhighlight lang="ecmascriptwren">import "os" for Process
import "./date" for Date
 
var args = Process.arguments
110

edits