I'm using Parse database. I store date format. when using console i get this format : Tue Jul 18 2017 15:46:47 GMT+0100 (CET)
I want to get this format : 18-07-2017.
Any idea please
It can be done by using following code:
let today = new Date();
let date=today.getDate() + "-"+ parseInt(today.getMonth()+1) +"-"+today.getFullYear();
console.log(date)
The moment library is awesome for all action/formatting that you want to do on date.
Have a look: https://momentjs.com/
You should do something like this:
import * as moment from 'moment'
const yourDate = new Date()
const NewDate = moment(yourDate, 'DD-MM-YYYY')
I have been able to get the result that you want in react-native like this:
import moment from 'moment'; // Import momentjs -->
https://momentjs.com
const maxDate = moment(new Date, 'DD-MM-YYYY').format(); //
15-11-2017T11:17:30+01:00
I hope this is useful for you.
This resolved formatted my date into dd/mm/yyyy
import moment from 'moment';
let date = moment(new Date()).format('DD/MM/YYYY')
import moment from 'moment';
var NewDate= moment(new Date, 'DD-MM-YYYY').format();
NewDate=NewDate.split('T')[0];
Related
I am wanting to display in a react native app a date given by the API.
The API is returning the following value:
"2022-06-20T14:00:00.000"
I need to display to the user
2:00 PM
The problem is that when I try this:
const dateString = "2022-06-20T14:00:00.000"
const date = new Date(dateString);
console.log(date)
The date has been changed to:
2022-06-20T14:00:00.000Z
Meaning it is 2pm UTC time when it should be 2pm local time.
On the end of the new Date variable, if you add the .toLocaleTimeString(), it should then output as 2:00:00PM.
const date = new Date(dateString).toLocaleTimeString();
you can use moment library with its localized formats,
here is the documentation for more of its formats https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format/
const dateString = "2022-06-20T14:00:00.000"
console.log(moment(dateString).format('LT'))
I am using vue3-datepicker library.
<datepicker
v-model="to_date"
inputFormat="yyyy-MM-dd"
/>
My data method is below:
data(){
to_date: new Date(),
}
When I am selecting a date and printing {{this.to_date}}, it is printing in Fri Sep 17 2021 09:39:49 GMT+0530 (India Standard Time).
I want it in yyyy-MM-dd format.
How can I achieve this?
A quick solution is to use Date.prototype.toISOString(), which formats the date as:
yyyy-MM-ddTHH:mm:ss.sssZ
So you get the desired date format by splitting the result by T, and taking the first array element:
const formatDate = date => {
const tzOffset = date.getTimezoneOffset() * 60 * 1000
return new Date(date - tzOffset).toISOString().split('T')[0]
}
Then use the method in your template:
<span>{{ formatDate(to_date) }}</span>
demo
You can achieve using moment npm package
install:-
npm i moment
example:-
moment(new Date()).format("yyyy-MM-dd")
you can use Use different format
var dt = new Date();
console.log(moment(dt).format("YYYY-MM-DD HH:mm:ss"));
console.log("Date: "+moment(dt).format("YYYY-MM-DD"));
console.log("Year: "+moment(dt).format("YYYY"));
console.log("Month: "+moment(dt).format("MM"));
console.log("Month: "+moment(dt).format("MMMM"));
when it comes to api, "createdAt": "2021-01-23 19:26:00", how do I convert it as "January 23", for example?
You can use intl package. Consider a code snippet like a below:
main() {
final DateTime now = DateTime.now();
final DateFormat formatter = DateFormat('yyyy-MM-dd');
final String formatted = formatter.format(now);
print(formatted); // something like 2021-06-19
}
There is a pretty straightforward way to read dates and write them in any format. Here is just one sample:
DateFormat.yMMMEd().format(DateTime.parse("2021-01-23 19:26:00"))
Here is the article about working with dates.
Here is the sample to localize the date with Intl package:
Intl.defaultLocale = 'es';
DateFormat.jm().format(DateTime.now());
You can either use moment
Moment.parse(createdAt).format(DATE_FORMAT); // replace DATE_FORMAT with the date format you need
Or you can do it yourself using intl package:
final DateFormat format = DateFormat.yMd('fr');
var formattedDate = format.format(DateTime.parse("2021-01-23 19:26:00"));
Install the intl package by running below command in your terminal:
dart pub add intl
Import the package in your class:
import 'package:intl/intl.dart';
main(){
final DateTime date = DateTime.now();
final String formatted = DateFormat.yMd().format(date);
print(formatted); // something like 06/21/2021
}
I hope you can help me, I need to show this format "2021-01-02" (YYY-MM-DD). but only show me 1 digit in month and day.
I have this in my app.js
var moment = require('moment');
getNow: function() {
const today = new Date();
const date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
const date2 = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+(today.getDate()-1);
const dateTime = date;
const dateTime2 = date2;
this.hoy = dateTime;
this.ayer = dateTime2;
And I have this in my blade
hoy #{{hoy}} - ayer #{{ayer}}
show me this in my output:
hoy 2021-1-2 - ayer 2021-1-1
but I need to the format be "2021-01-02" because I need 2 digits for compare months and days
PD: Sorry for my english.
Since you are using moment you can try:
const today = new Date();
this.hoy = moment(today).format('YYYY-M-D')
I am using V-Date-Picker in vuetify. I am showing multiple dates in date picker. The shown dates were passed from application. But the date picker is showing wrong date format.
let date = new Date(), context = this.$store.state[this.parentName], date_array=[];
date= moment(date).format(context.defaultDateFormat);
let monthStart = moment(date).startOf('Month').format(context.defaultDateFormat);
date_array.push(monthStart);
date_array.push(date);
let format_date = [];
format_date.push(moment(monthStart).format('YYYY-MM-DD'));
format_date.push(moment(date).format('YYYY-MM-DD'));
this.$store.state[this.parentName].fields.filter(x => x.value == this.fieldName).find(x => x).date = format_date.toString();
this.$store.state[this.parentName].editedItem.Date = date_array.toString();
context.picker = format_date.toString();
Please help me out
Please check by using https://github.com/date-fns/date-fns for formatting the date
Install
npm i date-fns
Usage
import format from 'date-fns/format'
import parseISO from 'date-fns/parseISO'
your v-model variable=format(parseISO(date from variable),'yyyy-MM-dd')
Formatting options are available on https://date-fns.org