var date = '28/05/2011 12:05';
var elem = date.split('');
hours = elem[0];
I have the above date format, please tell me how to split this, so that I can obtain 12 (hours) from this string?
var date = '28/05/2011 12:05';
var hrs = date.split(' ')[1].split(':')[0];
You can use a single call to split each component using a regular expression:
var date = '28/05/2011 12:05';
var elem = date.split(/[/ :]/);
alert(elem[3]); //-> 12
Working example: http://jsfiddle.net/gZ9c7/
A RegEx solution:
var myRe = /([0-9]+):[0-9]+$/i;
var myArray = myRe.exec("28/05/2011 12:05");
alert(myArray[1]); // 12
Some additional info:
Working code sample here.
About RegEx in JS.
As long as it's a consistent format:
var hours = date.split(' ')[1].split(':')[0]
is pretty easy.
when working with Dates it's better to use dedicated date/time functions:
var date = '28/05/2011 12:05';
var ms = Date.parse(date)
alert(new Date(ms).getHours())
Related
I am trying to get the pattern from a string in JavaScript, where my string is 01-01-2000 - 01-01-2010 here I want to make these both date separate like date1 = 01-01-2000 and date2 = 01-01-2010.
I have tried this code :
var date = "01-01-2000 - 01-01-2010";
var date2 = date.match(/([0-9]{2})-([0-9]{2})-([0-9]{4})/);
console.log(date2[0]);
output: 01-01-2000 [correct]
but I using console.log(date2[1]) it displaying 01 only.
Please help how to achieve the goal make this function.
expecting output :
date[0]: 01-01-2000
date[1]: 01-01-2010
You can give a try to String.matchAll() which returns an iterator of all results matching a string against a regular expression, including capturing groups.
Demo :
var date = "01-01-2000 - 01-01-2010";
var date2 = [ ...date.matchAll(/([0-9]{2})-([0-9]{2})-([0-9]{4})/g) ];
for (const match of date2) {
console.log(match[0])
}
You Can use split instead If your date format is fixed.
var date = "01-01-2000 - 01-01-2010";
var date2 = date.split(" - ")
console.log(date2);
if you want to work with pattern you only have to add the /g in the end for global search into the string:
var date = "01-01-2000 - 01-01-2010";
var date2 = date.match(/([0-9]{2})-([0-9]{2})-([0-9]{4})/g);
console.log(date2[0]);
console.log(date2[1]);
I'm trying to compare two dates using moments in my ReactJs App.
before compare two days,I have to converts dates into milliseconds.
i tried valueOf() but it's not returning milliseconds.how can i get milliseconds for below formated dates?
var s_date = moment(this.props.s_date).utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]");
var e_date = moment(this.props.e_date).utc().format("YYYY-MM-DDTHH:mm:ss.SSS[Z]");
var s_date = moment(this.props.s_date);
var e_date = moment(this.props.e_date);
var diff = e_date - s_date;
Try this:
moment(this.props.s_date).valueOf()
moment(this.props.e_date).valueOf()
I'm not sure how you used valueOf but by looking at the docs this should work:
var s_date = moment(this.props.s_date).utc().valueOf()
var e_date = moment(this.props.e_date).utc().valueOf()
pseudo code: e_date > s_date
After you compare dates then format like:
s_date.format("YYYY-MM-DDTHH:mm:ss.SSS[Z]")
BTW. you do not have to convert to miliseconds to compare dates. It would be enough to do:
if(s_date > e_date) {
} else {
}
First of all thanks in advance for helping, the community is great.
I have a problem parsing my date and time. Here is my code:
var date = mail.bodyText.match(/\=\= date \=\=\s*(.*[^\s*])/);
if (date) {
var string1 = date[1].match(/^\d{4}\-\d{2}-\d{2}/);
var string2 = date[2].match(\s(\d{2}\:\d{2}\:\d{2}));
var string3 = date[3].match(\s(\+\d{4}));
var parts1 = string1.split("-");
var parts2 = string2.split(":");
if (parts1 && parts2)
{
var dt = new Date(parseInt(parts1[0], 10), parseInt(parts1[1], 10) - 1, parseInt(parts1[2], 10), parseInt(parts2[3], 10), parseInt(parts2[4], 10), parseInt(parts2[5], 10));
}
date_final = dt;
}
date_final is defined elsewhere, and is in Date Time Picker format, and here is the input I am trying to parse:
blabla
== date ==
2016-02-13 16:22:10 +0200
blabla
Every time I execute the code, I get a parsing problem. The variable date_final cannot handle the parsed date. What do you think is missing from this code?
Update:
Here is what I'v etried out. Impossible for me to locate what's wrong:
var date = mail.bodyText.match(/\=\= date \=\=\s*(.*[^\s*])/);
if (date) {
var initial = date[1];
var formated = initial.substring(0, 19);
var final = formated.replace(/-/g, '/');
var last = new Date(final);
Field = last;
logging += "{date=" + Field + "}";
}
The code is actually parsing an email and sending the result over SSL. What surprises me the most is that the logs keep posting the following output of the date i naddition to the "parsing issue": date=Sat Feb 27 2016 16:22:10 GMT+0200 (CEST).
Do you think the problem comes from the code or could be related to how the appliance this code implemented on can handle it?
Thanks
Jane
Sorry for answering in comment.
Here's one solution to your question:
var dateStr = '2016-02-13 16:22:10 +0200';
// get yyyy-MM-dd HH:mm:ss
var formatedStr = dateStr.substring(0, 19);
// get yyyy/MM/dd HH:mm:ss in case of working on most of the browsers
var finalStr = formatedStr.replace(/-/g, '/');
// Date object can easily parse the datetime string we formated above
var date = new Date(finalStr);
Date object can parse complex strings.
Mail providers usually follow an RFC on how timestamps should be written, thus allowing other programming languages to heavily support it.
Just pass your string into date object and it will convert it for you.
let mailStr = `blabla
== date ==
2016-02-13 16:22:10 +0200
blabla`;
let regex = mailStr.match(/\=\= date \=\=\s*(.*[^\s*])/);
let dt = new Date(regex[1]);
console.log(dt);
The output is described in ISO-8601
I was trying to find the difference between two days, I'm getting NaN.
function formatDate(oldFormat,duration,timestamp){
var formattedDate = Utilities.formatDate(oldFormat, "IST","yyyy,MM,dd");
Logger.log(timestamp);
var newDate=new Date(timestamp*1000);
Logger.log(newDate);
newDate=Utilities.formatDate(newDate,"IST","yyyy,MM,dd");
Logger.log(formattedDate);
Logger.log(newDate);
var date1=new Date(formattedDate).getTime();
Logger.log(date1)
var date2=new Date(newDate).getTime();
Logger.log(date2)
var diff=daydiff(date2,date1);
Logger.log(diff); }
function daydiff(first, second) {
return (second-first)/(1000*60*60*24);}
How to find the difference between two date in days? I've date in this format :
date 1 : 2015,05,12
date 2: 2015,05,28
There is no point to use Utilities.formatDate() as it is meant to convert a normal date in to any format, not the other way round.
Also not sure what (oldFormat,duration,timestamp) stand for. You do not use duration in your script, and both dates you showed seem to be the same format.
If you are simply trying to find the difference between two dates, try this:
function formatDate(date1,date2){
date1 = new Date(fixDate(date1));
date2 = new Date(fixDate(date2));
var diff = (date2-date1)/(1000*60*60*24);
return(diff);
}
function fixDate(date){
var collector = date;
if (collector.match(",")!=null){
collector = collector.split(",");
var myString = [collector[1], collector[2], collector[0]].join("/");
return myString
}
}
I want to parse date in the format ddMMyyhhmm (eg 2804121530 representing 28th April 2012, 3:30 PM) to javascript Date() object.
Is there any oneliner solution to it? I'm looking for something of the kind:
var date = Date.parse('2804121530', 'ddMMyyhhmm');
or
var date = new Date('2804121530', 'ddMMyyhhmm');
Thanks for help!
A useful library here is DateJs. Just add a reference:
<script src="http://datejs.googlecode.com/files/date.js"
type="text/javascript"></script>
and use Date.parseExact:
var dateStr = '2804121530';
var date = Date.parseExact(dateStr, 'ddMMyyHHmm');
For a fast solution you can brake that string into pieces and create date from those pieces
function myDateFormat(myDate){
var day = myDate[0]+''+myDate[1];
var month = parseInt(myDate[2]+''+myDate[3], 10) - 1;
var year = '20'+myDate[4]+''+myDate[5];
var hour = myDate[6]+''+myDate[7];
var minute = myDate[8]+''+myDate[9];
return new Date(year,month,day,hour,minute,0,0);
}
var myDate = myDateFormat('2804121530');
or a simper solution:
function myDateFormat(myDate){
return new Date(('20'+myDate.slice(4,6)),(parseInt(myDate.slice(2,4), 10)-1),myDate.slice(0,2),myDate.slice(6,8),myDate.slice(8,10),0,0);
}
var myDate = myDateFormat('2804121530');
(new Date(1381344723000)).toUTCString()
Correct me if 'm worng...