My selected date is showing one day before in Ionic 4 - ionic-framework

I am working in my Ionic 4 app and I have used a native datepicker for that and I am converting the date to toISOString() but the problem is that it is showing one day before.
This is my ts:
this.datePicker.show({
date: new Date(),
mode: 'date',
androidTheme: this.datePicker.ANDROID_THEMES.THEME_HOLO_DARK,
}).then(
date => {
me.acceptchallengeform.setValue({
startchallenge: new Date(date).toISOString().split('T')[0],
});
console.log('Got date: ', date)},
err => {
console.log('Error occurred while getting date: ', err)}
);
var DatePickerDate ='Thu Aug 09 2018 00:00:00 GMT+0100 (British Summer Time)';
var myDate = new Date(DatePickerDate).toISOString().split('T')[0];
so myDate is now 2018-08-08
The problem is that it is showing the date one day before.
But I want to show the exact selected date.
Any help is much appreciated.

new Date() return the date using your system timezone.
toISOString() return the date in UTC and the time part of your date is midnight, so it returns Thu Aug 08 2018 23:00:00 (BST - 1h), that's why you're getting one day before.
What are your trying to do exactly?
Editing my answer following comment
in your case I would choose the easy way as you apparently don't need any timezone information:
startchallenge: date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
or you can use moment.js (http://momentjs.com/docs/#/displaying/format/)

Try This:
date = new Date(date.setDate(date.getDate() + 1));
me.acceptchallengeform.setValue({
startchallenge: new Date(date).toISOString().split('T')[0],
});
This will solve your problem.

Related

Truncate date to start of the day in a given timeZone

I am looking to truncate the date time to start of the day for the given timeZone.
If the current time is Mon Aug 24 15:38:42 America/Los_Angeles, it should be truncated to start of the day Mon Aug 24 00:00:00 America/Los_Angeles which then later should be converted to equivalent UTC time.
I have explored the methods provided by Joda Time Library, Apache Commons Library and ZonedDateTime but all of them truncate the date time in UTC and not to specific timeZone.
Can someone help me with this? Thanks in advance.
You can use ZonedDateTime. Use toLocalDate() on ZonedDateTime to get LocalDate then atStartOfDay on LocalDate with zone of ZonedDateTime instance to get the start of day.
Example:
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
ZonedDateTime startOfDay = now.toLocalDate().atStartOfDay(now.getZone());
DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
System.out.println(now.format(formatter)); // Mon, 24 Aug 2020 10:41:41 -0700
System.out.println(startOfDay.format(formatter)); // Mon, 24 Aug 2020 00:00:00 -0700
ZonedDateTime truncates in its own time zone. So it can be done a bit simpler than in the currently accepted answer (which is also a good and correct answer).
ZonedDateTime given = ZonedDateTime.of(2020, 8, 24, 15, 38, 42, 0, ZoneId.of("America/Los_Angeles"));
ZonedDateTime startOfDay = given.truncatedTo(ChronoUnit.DAYS);
System.out.println("Truncated to start of day: " + startOfDay);
Instant inUtc = startOfDay.toInstant();
System.out.println("In UTC: " + inUtc);
Output is:
Truncated to start of day: 2020-08-24T00:00-07:00[America/Los_Angeles]
In UTC: 2020-08-24T07:00:00Z

How can compare Date on mongo without time?

This is a sample data
[{ date: '2020-05-21T14:02:00.0123 }, { date: '2020-05-22T14:02:00.0123 }, { date: '2020-05-23T14:02:00.0123 }]
I want to filter records of 22-May or earlier, here is my expected:
[{ date: '2020-05-21T14:02:00.0123 }, { date: '2020-05-22T14:02:00.0123 }]
I tried with this query:
{ date: { $lte: new Date('2020-05-22') }}
But it returns only data earlier 22-May. I think problem is { date: { $lte: new Date('2020-05-22') }} will data.date lte 2020-05-22T00:00:00.000
How I can exclude time ?
You need to match type of input with type of date field in document, either both should be Date's or strings. I would highly suggest maintain dates as dates in DB. Also you need to know that dates in MongoDB are of format ISODate() and holds UTC date.
If your DB date field is of type date :
I want to filter records of 22-May or earlier
As you wanted to get documents <= 22-May, then sending new Date('2020-05-22') doesn't work. Cause :
when you do new Date('2020-05-22'), it will give you Fri May 22 2020 00:00:00 GMT only if you belong to UTC, for example if you're in New York America which is 4 hours behind UTC then it would result in Thu May 21 2020 20:00:00 GMT-0400 (Eastern Daylight Time) which represents EDT, basically it's your system/app server time i.e; local date time.
So if your region is behind UTC then you'll get a back date Thu May 21 2020 otherwise if it's ahead of UTC then there is no issue you'll see Fri May 22 2020.
Ok, now that we've fixed date issues, but we need to look into hours now :
Since you want docs <= 22-May then Fri May 22 2020 00:00:00 GMT doesn't work you need to have either <= Fri May 22 2020 23:59:59 GMT or Sat May 23 2020 00:00:00 GMT. In order to get that :
let date = new Date('2020-05-22')
date.setDate(date.getUTCDate()); // Setting utc date, Only useful if you're region is behind UTC
date = new Date(date.setHours(23,59,59,999)) // This overrides hours generated with 23:59:59 - which is what exactly needed here.
/** Now do your query */
{ date: { $lte: date }}
If your DB date field is of type string :
Then you don't need to convert string to date, instead you can send input date in string format :
let date = new Date('2020-05-22').toISOString() // 2020-05-22T00:00:00.000Z
/** Above would get you an ISO string no matter which region you're in, 
* now since we need `2020-05-22T23:59:59.000Z` which is not easy on ISO string
* We would just do +1 on date like `new Date('2020-05-23').toISOString()` - // 2020-05-23T00:00:00.000Z */
let date = new Date('2020-05-23').toISOString(); // like this
date = date.slice(0, -1) // removing `Z` from date string as your `date` field doesn't have this.
// Now your query is just `$lt`
{ date: { $lt: date }}
Test : mongoplayground

groovy next() date issue

I am trying to add a groovy script in SoapUI to find tomorrow's date using next() in current date.
I am getting the date as expected for all other dates except if the date is 19.
def TodaysDate = new java.util.Date().format("yyyy-MM-dd")
log.info ">>>>>>>>>> TodaysDate="+TodaysDate
log.info TodaysDate.next()
Output:
Wed Jul 19 14:34:29 EDT 2017:INFO:>>>>>>>>>> TodaysDate=2017-07-19
Wed Jul 19 14:34:29 EDT 2017:INFO:2017-07-1:
I tried this also.
def Today = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date())
log.info Today
NextDay = Today.next()
log.info NextDay
Output:
Wed Jul 19 14:43:38 EDT 2017:INFO:2017-07-19
Wed Jul 19 14:43:38 EDT 2017:INFO:2017-07-1:
This next() iterator works fine for other dates. Can you help me understand what I am doing incorrect here?
The format() method returns a String. And when you call next() on a String, it increments the last character. So, character 9 is incremented to the next unicode value, becoming :.
If you want your dates in a specific format, first you call next() in a Date object, then you format it:
def TodaysDate = new java.util.Date()
log.info ">>>>>>>>>> TodaysDate="+TodaysDate.format("yyyy-MM-dd")
log.info TodaysDate.next().format("yyyy-MM-dd")
The will print TodaysDate=2017-07-19 and the next date as 2017-07-20.
Maybe it's worth using TimeCategory in your case? Take a look at this simple code sample:
import groovy.time.TimeCategory
use (TimeCategory) {
println new Date() + 1.day
}
It works fine with any date. Test it with today's date (2017-07-19) - adding 1.day will give you 2017-07-20. Hope it helps.

Angular2 - convert unfortunately date

I have a problem - I use c# Web.api and Angular2. Thats works but Angular convert the date which I do not want / need. The date of the database is correct and angular add 1 hour
{{item.createdate | date:'H:mm' }}
So it shows 20:30 instead of 19:30 which is stored in the database :(
This is part of json repsone:
"createdate": "2016-11-29T19:30:00",
How can I solve this?
Thank you
Ralf
The Reason is there's no timezone information in the datetime string from the database result.
var date = new Date('2016-11-29T19:30:00');
console.log(date); //Tue Nov 29 2016 20:30:00 GMT+0100 (CET)
Written in the Angular2 documentation about the Date Pipe here.
The expression expects a valid datestring format:
YYYY-MM-DDThh:mmTZD (eg 2016-11-29T19:30+01:00)
var date = new Date('2016-11-29T19:30+01:00');
console.log(date); // Tue Nov 29 2016 19:30:00 GMT+0100 (CET)

Issue saving a string as ISO date format

I'm trying to save a date to MongoDB from FullCalendar in my Grails application.
I'm trying to parse the string 2015-12-27T00:00:00.000Z into the below format:
def startDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.start)
def endDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.end)
But, weirdly when I print the formatted date, I get Sun Dec 28 05:30:00 IST 2014. I don't know what or how that particular date is picked.
You should use lowercase y for year. Uppercase Y is for "Week year".
new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "2015-12-27T00:00:00.000Z")
===> Sat Dec 26 19:00:00 EST 2015
import java.text.SimpleDateFormat;
println new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX").parse("2018-07-30 09:57:15 +0800")