I have a date coming from database which is 2022-10-31 11:33:07.861Z, and in my js file I have this date is saved in a variable :
function test() {
let now = new Date().toLocaleString('en-GB', { timeZone: 'Europe/London'}); // I need now to be Europe/london time
let dateFromDb = db_date; // 2022-10-31 11:33:07.861Z
// I have a function which takes ^ above date and returns this : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
let formatedDate= this.toDate(dateFomDb); // returns : Mon Oct 31 2022 11:33:07 GMT+0000 (Greenwich Mean Time)
}
When I console.log(now, typeOf now) I get this: 02/11/2022, 23:24:52 string
and console for formatedDate returns this : Mon Oct 31 2022 11:33:07 GMT+0000 Object
I need to compare both dates :
return newDate <= now; // expect it to true or false;
Can anyone please suggest me how can I do this ?
I tried to cast both dates toLocalString but its string from I need object.
Related
1 - Make a function that given a date in text format "dd/mm/yyyy" returns a Date object with that date, using the Split() method.
2 - You have to do a "console.log() of the Date object created" and it should output something similar to
Mon Dec 2 2019 11:36:25 GMT+0100 (Central European Standard Time).
I have tried this but I don't know why when I write a console.log
I get the following:
2022-12-05T00:00:00.000Z
And I try to get it to appear something like this:
Mon Dec 2 2019 12:30:05 GMT+0100 (Central European Standard Time)
function convertDate(date) {
let dateArray = date.split("/");
let DateString = dateArray[2] + "/" + dateArray[1] + "/" + dateArray[0] + "Z";
let dateDate = new Date(dateString);
return dateDate;
}
console.log(convertDate("05/12/2022"));
you can use new Date() to get the date like "Wed Dec 28 2022 11:36:25 GMT+0100"
let date = new Date("05/12/2022");
console.log(date);
output will be Thu May 12 2022 00:00:00 GMT+0500 (Pakistan Standard Time) {}
How to convert Javascript Date Object to another timezone but the result must be Date object with the correct timezone
let date = new Date();
console.log(date);
date = date.toLocaleString('en-US', { timeZone: 'America/Vancouver' });
date = new Date(date);
console.log(date);
that gives the following result, the last result line (Date/Time) is correct but the time zone is incorrect which is still GMT-0500 (Colombia Standard Time) but must be GMT-0800 (Pacific Standard Time) timezone
Wed Jan 20 2021 00:14:11 GMT-0500 (Colombia Standard Time)
Tue Jan 19 2021 21:14:11 GMT-0500 (Colombia Standard Time)
You may try this :
let date = new Date();
console.log(date);
date = date.toLocaleString("en-CA", {
timeZone: "America/Vancouver",
timeZoneName: "long",
});
console.log(date);
Output:
Wed Jan 20 2021 09:18:16 GMT+0300 (Arabian Standard Time)
2021-01-19, 10:18:16 p.m. Pacific Standard Time
Once you get the correct TimeZone, you may change how the date and time are displayed by string manipulation if you need too.
Update:
This may not look pretty but i believe it should satisfy the requirements:
let date = new Date().toLocaleString("en-US", {
timeZone: "America/Vancouver",
timeZoneName: "short",
});
let date1 = new Date(date);
//adding a new property to Date object called tz and initializing it to null
Date.prototype.tz = null;
//stting the tz value to the Time zone output from toLocalString
date1.tz = date.slice(date.length - 3);
console.log(date1.toISOString() + " " + date1.tz);
console.log(date);
console.log(typeof date1);
Output:
2021-01-20T09:01:06.000Z PST
1/20/2021, 1:01:06 AM PST
Object
What i've done is create a new property of the object date to replace the built-in time zone property in Date, hence you get an object with a user specified Time zone.
Can someone please explain to me, why start and end variables print to console different values for these loggers?
I get printed out:
start1 = Mon Feb 17 2020 10:00:00 GMT+0100 (Central European Standard Time)
end1 = Mon Feb 17 2020 11:30:00 GMT+0100 (Central European Standard Time)
start2 = Mon Feb 17 2020 11:30:00 GMT+0100 (Central European Standard Time)
end2 = Mon Feb 17 2020 11:30:00 GMT+0100 (Central European Standard Time)
I want to add, that the code I presented is in some method of course, and the names start and end don't interfere with other variable names.
let start = this.service.getDateTime(
eventFromUi.start_date,
eventFromUi.start_time_hour,
eventFromUi.start_time_minute
);
console.log("start1 = " + start);
let end = this.service.getDateTime(
eventFromUi.start_date,
eventFromUi.end_time_hour,
eventFromUi.end_time_minute
);
console.log("end1 = " + end);
console.log("start2 = " + start);
console.log("end2 = " + end);
EDIT:
The getDateTime() method returns object of TypeScript ootb type Date.
I used Chrome debugger to look into this, and I see that when I first execute the getDateTime() method, I get value Mon Feb 17 2020 10:00:00 GMT+0100 (Central European Standard Time) returned and assigned to let start.
Then the method getDateTime() is executed again and retuns value Mon Feb 17 2020 11:30:00 GMT+0100 (Central European Standard Time), and this value gets assigned to both start and end variables.
How does this happen?
EDIT2:
Function getDateTime:
getDateTime(dateWithoutTime: Date, hour: number, minute: number): Date {
let date = dateWithoutTime;
console.log(date);
console.log(hour);
console.log(minute);
date.setHours(hour);
date.setMinutes(minute);
return date;
}
This is happening because you're dealing with object references.
getDateTime(dateWithoutTime: Date, hour: number, minute: number): Date {
let date = dateWithoutTime;
// date is now a copy of the *reference* to the same object that
// dateWithoutTime is a reference to. In other words: it is pointing
// to the same object in memory
date.setHours(hour);
date.setMinutes(minute);
// since date is pointing to the same object as dateWithoutTime, this is
// modifying both date and dateWithoutTime
return date;
}
This also means that in your code, eventFromUi.start_date, start and end are all references pointing to the same Date object.
To solve your problem, make sure you create a clone of dateWithoutTime when it is passed into your function:
getDateTime(dateWithoutTime: Date, hour: number, minute: number): Date {
let date = new Date(dateWithoutTime);
// date is now a reference to a *new Date object* with the same
// date/time/etc. values as dateWithoutTime
date.setHours(hour);
date.setMinutes(minute);
// since date now points to a new object, this is only modifying date
// while leaving dateWithoutTime alone and unchanged
return date;
}
I need to store an unix value from a Time input.
The problem is that :
// I create a Moment Date from my input :
var date = moment({hour: 10, minute: 00)
// gives this _d : Mon May 04 2015 10:00:00 GMT+0200 (CEST)
// I convert it to unix value
date = date.unix()
// -> 1430726400
moment( date ).format('HH:mm')
// -> "14:25" // Should give me 10:00
// Online conversion unix time gives me : Mon, 04 May 2015 08:00:00 GMT
So how can I keep my 10:00 in memory as unix value using those transformations ?
Per documentation:
moment.unix( date ).format('HH:mm')
I kind of sorted it this way :
// I create a Moment Date from my input :
var date = moment({hour: 10, minute: 00)
// gives this _d : Mon May 04 2015 10:00:00 GMT+0200 (CEST)
// Convert to unix
date = date.toDate();
date = date.getTime();
console.log(moment( date ).format('HH:mm')); // Gives me 10:00
I hope it will keep the good time ? In France the time changes twice a year ( go forward 1 hour, go backwards 1 hour)
I have a problem while trying to convert a specific type of date.
My goal is to get it into this format: dd/MM/yyyy
The current date format: Thu Apr 04 00:00:00 EEST 2013
When I alerted using JavaScript, it responds that it is not a date. I used many solutions like to format it on JSP:
<fmt:formatDate value="${theDate}" pattern="dd/MM/yyyy"/>
Result error:
Attempt to convert String "Thu Apr 04 00:00:00 EEST 2013" to type "java.util.Date", but there is no PropertyEditor for that type.
And even in Javascript:
var dateCreation = new Date(theDate);
The problem in JavaScript is that it says that dateCreation is not a date. Any ideas?
Problem with your date string "Thu Apr 04 00:00:00 EEST 2013"
when i tried with your date string got IllegelArgumentException
change "Thu Apr 04 00:00:00 EEST 2013" to "Thu Apr 04 00:00:00 EST 2013" then working fine.
I'm unable to guess why you got that EEST (one E extra).
public static void main(String[] args) {
Date date = new Date("Thu Apr 04 00:00:00 EST 2013");
System.out.println(date);
}
You are getting error in javascript because your passing the same string to JS i guess.
fmt:formatDate expects a java date object as value. In your case looks like theDate is not a date object but a String representation of date object. Please provide a date object and that should work.
Also in JavaScript what is the value of theDate param and where is it defined?
The date object in JavaScript has the following constructors:
var d = new Date();
var d = new Date(milliseconds); // from epoch
var d = new Date(dateString); // "yyyy-mm-dd hh:mm:ss"
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
Make sure you use one of them. Bear in mind that the month is 0-based for the last mentioned constructor.
EST is GMT-05:00 while EEST is GMT+03:00. The difference occurs e.g. when parsing "Thu Apr 04 23:00:00 EEST 2013". Java (7) can parse EEST, but it can not format in EEST.
String s = "Thu Apr 04 23:00:00 EEST 2013";
SimpleDateFormat sdfParse = new SimpleDateFormat("E MMM d H:m:s z yyyy", Locale.ENGLISH);
Date d = sdfParse.parse(s);
SimpleTimeZone stz = new SimpleTimeZone(3 * 3600000 /* GMT+03:00 */, "EEST");
GregorianCalendar cal = new GregorianCalendar(stz, Locale.ENGLISH);
cal.setTime(d);
String ddmmyyyy = String.format("%02d/%02d/%4d", cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR));
System.out.println(s+"\n"+ddmmyyyy);
In JavaScript:
function format2(s) {s="0"+s;return s.substr(s.length-2);}
function dateFormat(theDate) {
//theDate = "Thu Apr 04 00:00:00 EEST 2013";
var timestamp = Date.parse(theDate.replace(/EEST/, "GMT+0300"));
var datePseudoGMT = new Date(timestamp+3 * 3600000);
var dateCreation = format2(datePseudoGMT.getUTCDate())
+"/"+format2(datePseudoGMT.getUTCMonth()+1)
+"/"+format2(datePseudoGMT.getUTCFullYear());
console.log(dateCreation);
return dateCreation;
}