I have an input date string like so "30/09/1992", and I found this code to suit my need. PFB the code.
var input1 = "30/09/1992";
var isVaidDate = false;
var actualDate = "";
try{
var pattern = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var arrayDate = input1.match(pattern);
var actualDate = new Date(arrayDate[3], arrayDate[2] - 1, arrayDate[1]);
var isVaidDate = typeof dt.getMonth === 'function';
}catch(e){var output1 = false;}
print(isVaidDate);
print(actualDate);
The above code works fine but when I set the input as "31/09/1992" or "40/09/1992" I am expecting invalid date to come but I get the below output.
for "31/09/1992":
true
Thu Oct 01 1992 00:00:00 GMT+0530 (India Standard Time)
for "40/09/1992":
true
Thu Oct 10 1992 00:00:00 GMT+0530 (India Standard Time)
How should I get this to fail when i pass these two strings. Thanks. Also what is going on and why it didnt fail, would also be useful :)
This example can help you:
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
Here you can verify each variable as day, month and year.
Related
i want to convert utc time to ist time , im getting time like this. i want to remove "GMT+0530 (India Standard Time)"
Thu Jul 19 2018 18:06:14 GMT+0530 (India Standard Time)
Expected result
Jul 19 2018 18:06:14 or 19/7/2018 06:06 PM
sample data
2018-07-19T16:36:21.065Z
code
function UtcToIst(data) {
var dt = new Date(data);
return dt;
}
I achieved it in the following way. Please correct me, if I am wrong.
var data = '2018-07-19T16:36:21.065Z';
function UtcToIst(data) {
var dt = new Date(data);
return dt;
}
var updDate = UtcToIst(data).toLocaleDateString();
var updTime = UtcToIst(data).toLocaleTimeString();
var updDateTime = updDate + ", " + updTime;
console.log(updDateTime); // 7/19/2018, 9:36:21 AM
I am displaying current day,month,date,year and time like this
Mon Oct 24 2016 17:09:25 GMT+0530 (India Standard Time)
but i need to display like this
Mon Oct 24 2016 17:09:25
my code in javascript:
var timestamp = new Date();
editor.insertHtml( 'The current date and time is: ' + timestamp.toString());
How can i do this please can anyone tell me how to do this.
Thank you
If you are open to add a library, you should use moment.js
console.log(moment().format('ddd MMM DD YYYY hh:mm:ss'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
If not, a small work around
var d = new Date().toString();
var index = d.lastIndexOf(':') +3
console.log(d.substring(0, index))
Note: moment approach is more preferred
var date = new Date();
var n = d.toLocaleString();
document.getElementById("demo").innerHTML = n;
This is work for me.
var timestamp = new Date();
console.log(
timestamp.toString().split('GMT')
)
// Mon Oct 25 2021 17:56:11 GMT+0530 (India Standard Time)`
the Output will be Mon Oct 25 2021 17:55:02
let today = new Date();
today = today.toString();
today = today.split('G')[0];
console.log(today);
i am getting the format from server is : Mon Sep 05 2016 00:00:00 GMT+0530 (India Standard Time)
Some times I am getting 05/09/2016.(dd/MM/yyyy)
How can check whether i am getting Mon Sep 05 2016 00:00:00 GMT+0530 (India Standard Time) this format?
Thanks in advance!
I assume irrespective of the format, you want to create date object and use it. For such situations, you should use moment.js. But if not, you can try something like this:
function getDateFromString(str) {
var date1 = null;
if (str.trim().indexOf(" ") > -1)
date1 = new Date(str);
else {
var seperator = null;
if (str.indexOf("/") > -1)
seperator = "/"
else
seperator = "-"
str = str.split(seperator).reverse().join(seperator);
console.log(str)
date1 = new Date(str);
}
return date1;
}
var a = "Mon Sep 05 2016 00:00:00 GMT+0530 (India Standard Time)";
var b = "05/09/2016";
alert(getDateFromString(a));
alert(getDateFromString(b));
The code below mentioned is for comparision of date. Both date1 and mydate have similar values,But if i compare its not entering if loop. Any help appreciated
var date_arr = new Array( "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var Avl_date = document.getElementById("Available_Date").value;
var V_date1 = Avl_date.split('-');
var date1 = new Date (V_date1[2], date_arr.indexOf(V_date1[1]),V_date1[0]);
var myDate = new Date();
myDate.setHours(0,0,0);
//Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> date1
//Thu Dec 04 2014 00:00:00 GMT+0530 (IST) --> mydate
if(myDate.getTime() === date1.getTime())
{
//Not entering the loop
}
You're not setting the milliseconds of myDate to 0, so it keeps its original milliseconds. Use:
myDate.setHours(0,0,0,0);
Barmar is correct. setting the milliseconds will solve your problem.
I have two variables namely
date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
When I compare the two dates I get that date2 is greater which I need is correct. But I do not want to check the time part of the two dates I have. How could I get the date part alone from these two dates and compare it?
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
if(d>=today){ //I need to check the date parts alone.
alert(d is greater than or equal to current date);
}
Try clearing the time using Date.setHours:
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
Example Code:
var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);
if(d >= today){
alert(d is greater than or equal to current date);
}
The best way would be to modify the accepted answer's if statement as follows
if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))
In this way, you can easily check for equality as well because the return type for setHours() is integer.
Try:
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
var d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());
if(dDateOnly>=todayDateOnly){
alert(d is greater than or equal to current date);
}
var StartDate = $("#StartDate").val();
var EndDate = $("#EndDate").val();
if ((( EndDate - StartDate)/ (86400000*7))<0)
{
alert("Start Date Must Be Earlier Than End Date"); $("#StartDate").focus();
error = true;
return false;
}