Format JavaScript date as yyyy-mm-dd
You can do:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate('Sun May 11,2014'));
How do I get a date in YYYY-MM-DD format?
Just use the built-in .toISOString()
method like so: toISOString().split('T')[0]
. Simple, clean and all in a single line.
var date = (new Date()).toISOString().split('T')[0];
document.getElementById('date').innerHTML = date;
<div id="date"></div>
convert date in YYYY-MM-DD in javascript
You could use these methods from the date object and format it yourself
function get_date_of_last_n_days_from_today(n) {
const date = new Date();
date.setDate(date.getDate() - n);
const pad = n => n.toString().padStart(2, '0');
const yyyy = date.getFullYear(),
mm = pad(date.getMonth() + 1),
dd = pad(date.getDate());
return `${yyyy}-${mm}-${dd}`;
}
console.log(get_date_of_last_n_days_from_today(3));
How do I format a date in JavaScript?
For custom-delimited date formats, you have to pull out the date (or time)
components from a DateTimeFormat
object (which is part of the
ECMAScript Internationalization API), and then manually create a string
with the delimiters you want.
To do this, you can use DateTimeFormat#formatToParts
. You could
destructure the array, but that is not ideal, as the array output depends on the
locale:
{ // example 1
let f = new Intl.DateTimeFormat('en');
let a = f.formatToParts();
console.log(a);
}
{ // example 2
let f = new Intl.DateTimeFormat('hi');
let a = f.formatToParts();
console.log(a);
}
How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
If you're using Node.js, you're sure to have EcmaScript 5, and so Date has a toISOString
method. You're asking for a slight modification of ISO8601:
new Date().toISOString()
> '2012-11-04T14:51:06.157Z'
So just cut a few things out, and you're set:
new Date().toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, '') // delete the dot and everything after
> '2012-11-04 14:55:45'
Or, in one line: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
ISO8601 is necessarily UTC (also indicated by the trailing Z on the first result), so you get UTC by default (always a good thing).
How to convert YYYY-MM-DD format to Date object
You can do new Date('2022-02-2017')
.
I want to change the date format to yyyy-mm-dd
With PHP get date of today and change format
$origDate = "2019-01-15";
$newDate = date("m-d-Y", strtotime($origDate));
echo $newDate;
Output
01-15-2019
With js
var date = new Date();
var year=date.getFullYear();
var month= date.getMonth();
var date=date.getDay();
console.log(` ${day}:${month}:${year}`);
How to Format Date to Complete YYYY-MM-DD-HH-MM-SS
You can get YYYY-MM-DD-HH-MM-SS format like this:
new Date(d.getTime() - (d.getTimezoneOffset() * 60000)).toISOString().split(".")[0].replace(/[T:]/g, '-')
Explanation:
.split(".")[0]
removes the dot near the end and everything after it..replace(/[T:]/g, '-')
changes theT
and the colons (:
) to dashes.
Format current date time to yyyy-MM-dd'T'HH:mm:ss.SSSZ in Angular/Javascript/Typescript
There are two ways to achieve this:
- The Angular way using
DatePipe
In you component, you can inject DatePipe
from @angular/common
and call on the transform
method to format date. Here is the list from Angular docs of options you can use to format date and time.
import { DatePipe } from '@angular/common';
class AppComponent {
constructor(private datePipe: DatePipe) {}
someMethod() {
const date = this.datePipe.transform(new Date(), 'yyyy-MM-ddThh:mm:ss.SSSZ');
}
}
In the module, where you have defined this component, you need to provide DatPipe
as providers
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent],
bootstrap: [ AppComponent ],
providers: [ DatePipe ]
})
export class AppModule { }
- Second way would be to use native
toISOString()
method
const date = (new Date()).toISOString();
Related Topics
Loading Cross-Domain Endpoint With Ajax
Find the Min/Max Element of an Array in JavaScript
JavaScript by Reference Vs. by Value
Get All Non-Unique Values (I.E.: Duplicate/More Than One Occurrence) in an Array
Http Get Request in JavaScript
Stop Setinterval Call in JavaScript
How to Detect If a Browser Window Is Not Currently Active
Direct Vs. Delegated - Jquery .On()
What Is the Purpose of a Self Executing Function in JavaScript
Download File Using JavaScript/Jquery
Sorting in JavaScript: Shouldn't Returning a Boolean Be Enough For a Comparison Function
Counting the Occurrences/Frequency of Array Elements
Setstate Doesn't Update the State Immediately
How to Replace Plain Urls With Links
When Should I Use Double or Single Quotes in JavaScript
Use Async Await With Array.Map
How to Create an Object Property from a Variable Value in JavaScript