This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #99 Format date YYYY-MM-DD
Assign to the string x the value of the fields (year, month, day) of the date d, in format YYYY-MM-DD.
- Ada
- Clojure
- Cobol
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- JS
- JS
- Java
- Java
- Java
- Lua
- Obj-C
- PHP
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Smalltalk
- VB
X : constant String :=
Ada.Calendar.Formatting.Image (D) (1 .. 10);
The Image function returns time as well therefore the slice.
IDENTIFICATION DIVISION.
PROGRAM-ID. date format.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 curr-date.
03 yyyy pic 9(4).
03 mm pic 9(2).
03 dd pic 9(2).
01 d.
03 year pic 9(4).
03 FILLER pic x VALUE '-'.
03 month pic 99.
03 FILLER pic x VALUE '-'.
03 day pic 99.
PROCEDURE DIVISION.
MOVE FUNCTION CURRENT-DATE TO curr-date
MOVE yyyy to year
MOVE mm to month
MOVE dd to day
DISPLAY d
STOP RUN.
int main()
{
char x[32]{};
time_t a = time(nullptr);
struct tm d;
if (localtime_s(&d, &a) == 0) {
strftime(x, sizeof(x), "%F", &d);
std::cout << x << std::endl;
}
return 0;
}
//Microsoft's localtime_s, returns zero on success, an error code on failure.
let y = d.getFullYear(),
m = d.getMonth() + 1,
D = d.getDate(), x
m = m.toString().padStart(2, '0')
D = D.toString().padStart(2, '0')
x = `${y}-${m}-${D}`
The `getMonth` method is zero-based—e.g., January is 0, February is 1, etc.
String x = String.format("%1$tY-%1$tm-%1$td", d)
Per documentation (see documentation URL), d may be:
long, Long, java.util.Date, java.util.Calendar, or java.time.temporal.TemporalAccessor (which then includes most java.time types)
long, Long, java.util.Date, java.util.Calendar, or java.time.temporal.TemporalAccessor (which then includes most java.time types)
Date d = getInstance().getTime();
String s = "%tY-%<tm-%<td".formatted(d);
// once
static NSDateFormatter *df;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
df=[[NSDateFormatter alloc] init];
df.locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
df.timeZone=[NSTimeZone timeZoneForSecondsFromGMT:0];
df.dateFormat=@"yyyy-MM-dd";
});
// then, wherever needed
NSString *x=[df stringFromDate:d];
Most time, it's much better to use user-defined system-wide date formats (through dateStyle and timeStyle) instead of fixed ones.