I have a requirement to validate start-date < end-date, but the date formats changes based on the regional settings.
User can change region of site at any time when user changes to different date formats by validation fails.
I get error if the format is of 19-2-1
function getFormattedDate(datestr) {
var year = date.getFullYear();
var month = (1 + date.getMonth()).toString();
month = month.length > 1 ? month : '0' + month;
var day = date.getDate().toString();
day = day.length > 1 ? day : '0' + day;
return month + '/' + day + '/' + year;
}
//date format for all regions (mm-dd-yyyy, dd-mm-yyy, d mm yyyy, mm dd yyyy, mm/dd/yyyy, dd/mm/yyyy, dd.mm.yyyy, mm.dd.yyyy)
if (isNaN(StartDateEnfant.valueOf()) || isNaN(OData__EndDateEnfant.valueOf())) {
StartDateEnfant = new Date(startDate.replace(/(\d{2})[- /.](\d{2})[- /.](\d+)/, "$2/$1/$3"));
OData__EndDateEnfant = new Date(endDate.replace(/(\d{2})[- /.](\d{2})[- /.](\d+)/, "$2/$1/$3"));
}
I get wrong validation if start date is 3.1.2019(jan 3 2019) and end date if 10.1.2019 ( jan 10 2019) but it takes as sep 10 2019 which is wrong
Related
My database value is this
2020-03-08 20:44:00
But in javascript. It display
Mon Mar 09 2020 09:44:00 GMT+0800 (Singapore Standard Time)
Want i want to display on UI
2020-03-08 20:44:00
or
2020-03-08
Is there a way to remove the timezone and get only the actual value from the database.
toISOString is not a proper way to get date into DateTime. please follow the below method to get a date from DateTime.
var date = new Date("2020-03-08 20:44:00");
var year = date.getFullYear();
var month = (1 + date.getMonth()).toString();
month = month.length > 1 ? month : '0' + month;
var day = date.getDate().toString();
day = day.length > 1 ? day : '0' + day;
var newDate = year + '-' + month + '-' + day;
console.log("Date plush time - "+date);
console.log("Only Date - "+newDate);
You're using the silently using Date object's .toString() method which converts the UTC date (that your database is storing) into a time in the current time zone.
If date is the variable that you get from your database, then you can format it like you want it like this:
let dateString = date.toISOString().replace('T', ' ').replace(/\..+/, '')
This will take your date, convert it into an ISO string (in the form 2020-01-10T03:09:24.551Z) and replace the T with a space and everything after the decimal with nothing.
Try this.
let d = new Date('2020-03-08 20:44:00');
console.log(`${d.getFullYear()}-${d.getMonth() < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1}-${d.getDate() < 10 ? '0' + (d.getDate()): d.getDate()}`);
You can take each part of the date and construct your own format
example:
let formatted_date = my_date.getFullYear() + "-" + (my_date.getMonth() + 1) + "-" + my_date.getDate()
in this example: my_date hold the date you want to display.
If you're able to use a library, use moment.js
https://momentjs.com/docs/#/displaying/
moment("2020-03-08 20:44:00").format("YYYY-MM-DD");
or
moment(new Date("2020-03-08 20:44:00")).format("YYYY-MM-DD");
It can even change the time to utc
https://momentjs.com/guides/#/parsing/local-utc-zone/
moment.utc("2020-03-08 20:44:00").format("YYYY-MM-DD");
hope this helps :)
Subtract your timezone offset milliseconds.
var dt = new Date('2020-03-08 20:44:00');
dt = new Date(dt.getTime()-dt.getTimezoneOffset()*60000);
console.log(dt.toUTCString());
var mo = dt.getUTCMonth()+1, d = dt.getUTCDate(), h = dt.getUTCHours();
var m = dt.getUTCMinutes(), s = dt.getUTCSeconds();
if(mo < 10)mo = '0'+mo;
if(d < 10)d = '0'+d;
if(h < 10)h = '0'+h;
if(m < 10)m = '0'+m;
if(s < 10)s = '0'+s;
console.log(dt.getUTCFullYear()+'-'+mo+'-'+d+' '+h+':'+m+':'+s);
I have a date in format like that -
date= "2 march 2018"
I want to convert it to the format "20180302". i am using following method for that :
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
return [year, month, day].join('');
Everything works fine except that if my date has no date or month, it should return empty value in place of date and month. For example: If my date input is
date= "march 2018"
than, day = '' + d.getDate() should return empty. if date is "2018" than month = '' + (d.getMonth() + 1) and day = '' + d.getDate() should return null. I know it is a bad practise,but in my application I need to check if date and month is present of not..
var date= "2 march 2018";
var sp_date=date.split(" ");
var d = new Date(date);
if(sp_date.length==3){
month = '' + (d.getMonth() + 1);
day = '' + d.getDate();
year = d.getFullYear();
}else if(sp_date.length==2){
if(isNaN(Number(sp_date[0]))===false){
day = sp_date[0];
month='';
year=sp_date[1];
}else{
day ='';
month = '' + (d.getMonth() + 1);
year = d.getFullYear();
}
}else if(sp_date.length==1){
day ='';
month = '';
year = d.getFullYear();
}
return [year, month, day].join('');
You can do it with a single line using moment.js, Check the jsfiddle link below
http://jsfiddle.net/rLjQx/46813/
var date = moment("2 march 2018").format('YYYYMMDD');
I have a date that I want converted to a number in the format of yyyymmdd.
it comes in as
2017-08-16T05:47:42.070Z
I convert it as
let dt = new Date(dte)
which creates the date in dt of:
Wed Aug 16 2017 15:47:42 GMT+1000 (E. Australia Standard Time)
Now when I look at the parts I get the following:
dt.getFullYear() = 2017
dt.getMonth() = 7
dt.getDay() = 3
How come the day and month are off?
getMonth()
returns an integer number, between 0 and 11, representing the month in the given date according to local time. 0 corresponds to January, 1 to February, and so on.
getDay()
returns an integer number corresponding to the day of the week for the given date, according to local time: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on.
This means that result of your code is correct
dt.getFullYear() = 2017
dt.getMonth() = 7
dt.getDay() = 3
There is a function called getDate() that will return 16 for your case.
These functions are from native javascript Date Object.
You can do it like this:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate("2017-08-16T05:47:42.070Z"));
Following code return result for you:
var dte = '2017-08-16T05:47:42.070Z';
var dt = new Date(dte);
var myFrmt = dt.getFullYear().toString() + (dt.getMonth()+1).toString() + dt.getDate().toString();
alert(myFrmt);
Found out an easy way (don't know why the above doesn't work!!) using Moment
Moment(dt).format('YYYYMMDD').toString()
gives the answer: 20170816 !!
I need to use JavaScript to covert a date to to from '2012-09-15T00:00:00' to a CCYYMMDD format? How can I do this?
I don't see what .NET and XSLT have to do with your question.
You could use the Date constructor to parse the ISO 8601 encoded string into a javascript Date object:
var dateStr = '2012-09-15T00:00:00';
var date = new Date(dateStr);
and then build the desired format:
var year = '' + date.getFullYear();
var month = date.getMonth() + 1;
month = month < 10 ? '0' + month : month;
var day = '' + date.getDate();
day = day < 10 ? '0' + day : day;
var formattedDate = year + month + day;
And here's a live demo.
Using replace and split.
var date = '2012-09-15T00:00:00';
date = date.replace(/-/,"").split("T")[0];// date will be 20120915
I have an string that contains month/date and I need to insert the year. The string looks like:
Last Mark:: 2/27 6:57 PM
I want to convert the string to something like:
Last Mark:: 2010/02/27 18:57
In this case, there will not be any entries more than a year old. For example, if the date were 10/12 it can be assumed that the year is 2009.
What is the best method for this?
Following from Adam's suggestion:
function convertDate(yourDate) {
var today = new Date();
var newDate = new Date(today.getFullYear() + '/' + yourDate);
// If newDate is in the future, subtract 1 from year
if (newDate > today)
newDate.setFullYear(newDate.getFullYear() - 1);
// Get the month and day value from newDate
var month = newDate.getMonth() + 1;
var day = newDate.getDate();
// Add the 0 padding to months and days smaller than 10
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
// Return a string in YYYY/MM/DD HH:MM format
return newDate.getFullYear() + '/' +
month + '/' +
day + ' ' +
newDate.getHours() + ':' +
newDate.getMinutes();
}
convertDate('2/27 6:57 PM'); // Returns: "2010/02/27 18:57"
convertDate('3/27 6:57 PM'); // Returns: "2009/03/27 18:57"
the code for adding THIS year is simple
var d = Date();
var withYear = d.getFullYear() + yourDate;
however, the logic behind considerating if it should be this year or last year could be harder to do
I would think this way: get today's date. If the date is higher than today's, it's last year, so add d.getFullYear()-1, otherwise add d.getFullYear()
This returns the current year:
var d = new Date();
var year = d.getFullYear();
To figure whether its this year or not you could just compare the day and month with the current day and month, and if necessary, subtract 1 from the year.
To get the day and month from the Date object:
d.getMonth(); // warning this is 0-indexed (0-11)
d.getDate(); // this is 1-indexed (1-31)