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

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

Related

Cucumber-java-Startdate shouldn't be todays date in Java script

I am new to this code.
While running automation script, the date is automatically picked todays date.
actually the date starts from 01/01/2022.
How to change this code?
public void validateStartDateDefaultValueNew() throws Exception{
Date date = Calendar.getInstance().getTime();
DateFormat date formatter = new SimpleDateFormat( pattern: "dd/MM/yyyy");
String today = dateformatter.format(date);
System.out.println("today is "+today);
System.out.println("formatted today is "+date formatter.parse(today))
String [] actual1 = searchStartDateInput.getAttribute("value").split(regor " ");
String actual actuall[0]; =
System.out.println("today actual app is [+actual);
Assert.assertEquals(today, actual);
In your code, you mentioned -
'Date date = Calendar.getInstance().getTime();'.
This line of code takes today's date and current time.
If you want the start date to be '01/01/2022', then hard-code it in your code.

js-joda convert UTC zoned datetime into local datetime

I'm struggling to convert between a parsed UTC datetime into a LocalDateTime (America/New_York), with the hours adjusted accordingly between the two timezones, using the js-joda library.
In the javascript native Date object, what's stored is in UTC and is adjusted for the local (system) timezone at the time of display (.toString(), etc). I understand js-joda works differently, so I am assuming it's my own misunderstanding of just how js-joda timezones work. Regardless, I can't seem to get any kind of DateTime to report/show that it's 8am in the morning in New York, given a UTC time of 12pm noon.
Below is the Jest test-code to show what's been attempted so far.
import {
DateTimeParseException,
LocalDateTime,
ZonedDateTime, ZoneId,
} from '#js-joda/core';
import '#js-joda/timezone';
// (Jest tests)
test('converting a utc zoned-datetime into a local datetime', () => {
expect(ZoneId.systemDefault().id().toString()).toMatch('America/New_York');
// America/New_York is currently -04:00 from UTC (daylight savings on)
const utcStr1 = "2022-09-27T12:00:00Z";
const utcStr2 = "2022-09-27T12:00:00+00:00";
const utcDT = ZonedDateTime.parse(utcStr1);
// make sure I understand how parsing UTC forms work:
expect(utcDT).toEqual(ZonedDateTime.parse(utcStr2)); // OK
expect(utcDT.zone().id()).toEqual(ZoneId.UTC.id()); // OK
// trying to parse a UTC datetime string using LocalDateTime fails,
// so that won't work...
expect(() => LocalDateTime.parse("2022-09-27T12:00:00Z")).toThrow(DateTimeParseException);
expect(() => LocalDateTime.parse("2022-09-27T12:00:00+00:00")).toThrow(DateTimeParseException);
/*
How do I convert this UTC datetime into a LocalDateTime, adjusting for UTC (hr:12) -> America/New_York (hr:8)?
All of the below attempts fail, returning 12.
*/
expect(utcDT.toOffsetDateTime().hour()).toEqual(8); // FAILS: .hour() returns 12 not 8
expect(utcDT.toLocalDateTime().hour()).toEqual(8); // FAILS: .hour() returns 12 not 8
expect(utcDT.withZoneSameLocal(ZoneId.systemDefault()).hour()).toEqual(8); // FAILS: .hour() returns 12 not 8
});
The function to use is withZoneSameInstant:
const localDateTime = utcDT.withZoneSameInstant(ZoneId.systemDefault());

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 do you compare selector attributes in Testcafe?

I'm trying to compare the date of videos on a webpage to today's date. If the difference between the two dates is more than X days, report back as false.
The videos on the webpage have a tag in them which uses the format yyyy-mm-dd
I've got a selector set up to find the videos const videoDate = Selector('OPTA-video').withAttribute('data-secondary-time')
Now how do I set a variable to today's date and compare the two? I'm completely stuck!
I was using Katalon Studio before and here's the groovy script that did the same job:
String videoDate = WebUI.getAttribute(findTestObject('OPTA-video'), 'data-secondary_time')
LocalDate todaysDate = LocalDate.now()
LocalDate videoDateParsed = LocalDate.parse(videoDate, dtf)
if (ChronoUnit.DAYS.between(videoDateParsed, todaysDate) > 1) {
KeywordUtil.markFailed('The videos are 2+ days old.')
} else {
KeywordUtil.logInfo('The videos are up to date.')
}
You can use the getAttribute TestCafe method to access an attribute value. Then, parse the attribute value into the JavaScript Date object:
String videoDate = Selector('OPTA-video').getAttribute('data-secondary-time');
Date videoDateParsed = Date.parse(videoDate);
Date todaysDate = Date.now()
...
In the following thread you can find how to compare Date objects.
This is one of the scripts that I am using.
//getting your XPath test value into a string
String ann_time =
WebUI.getText(findTestObject("ObjectRepository/navigateTOElement/announcements_date"))
//converting time to simple date format
SimpleDateFormat sdf = new SimpleDateFormat('HH:mm')
Date sdf_anntime = sdf.parse(new String(ann_time))
//getting Current time
SimpleDateFormat dateFormatGmt = new SimpleDateFormat('HH:mm')
dateFormatGmt.setTimeZone(TimeZone.getTimeZone('GMT'))
SimpleDateFormat dateFormatLocal = new SimpleDateFormat('HH:mm')
currDate = dateFormatLocal.parse(dateFormatGmt.format(new Date()))
// time gap in long format
long duration = currDate.getTime() - sdf_anntime.getTime()
//time gap to mins
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration)
//compare time gap with globale variable
if (diffInMinutes < GlobalVariable.News_updated_time) {
log.logInfo("system is getting updated,last updated "+ diffInMinutes + "min ago")
} else {
CustomKeywords.'errorMessage.logFailed.markStepFailed'('from 1 h, system was not updated')
log.logInfo('from '+ diffInMinutes+ 'h, system was not updated')
}