Format vue-datepicker time in yyyy-mm-dd - vue.js

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"));

Related

How do I return the local time portion of a string given a date string

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'))

Nest Js Typeorm Query

I am trying to run a query at the repository level of my nestjs application.
The date format of the column (runDateTime) in my DB is isoString. I want to find all the records that have runDateTime of today's date.
I have a function called Parsebackenddate that essentially converts iso string to YYYY-MM-DD. how do i use this function with "schedule.runDateTime" in order to compare both dates in the format YYYY-MM-DD?
Or is there an alternative?
if (getToday) {
const todayDate = dayjs().format('YYYY-MM-DD');
query.where('schedule.runDateTime = :todayDate', {
todayDate,
});
``
Much appreciated.
first, you have to get a date from the database and hold it in a class variable then compare.
something like that may this could help.
var _orderBean = new date_beans();
_orderBean.dueDate = _response[a].dueDate.toISOString().slice(0,10); //YYYY-MM-DD
then compare
var d = new Date(); //todaydate
if(d.toISOString().slice(0,10)>_orderBean.dueDate)
// TodayDate > DueDate

Vuetify v-Date-Picker not showing correct date inside date picker calendar

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

How to get DD-MM-YYYY format date

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];

Unit test a date function

I need to create a unit test for the following function:
export default date => {
const dateInMs = new Date(date).getTime(); // UTC date
const clientDate = new Date();
// timezone offset is in minutes therefore it is being converted to milliseconds
const offset = clientDate.getTimezoneOffset() * 60 * 1000;
return new Date(dateInMs - offset).toISOString();
};
I know I should pass the test a date in the following format
2017-07-01T00:00:00+00:00
Can someone provide some guidance as to how to write a simple test with this information.