I am getting a date and a time from 2 input boxes with datebox plugin.
I have the date as "16-01-2015" and time as "11:37 AM". I need to send both to my server to add to the database but my database need the date in format: "2015-01-16 21:11:00"
How can I convert my both strings in a date or a string with the other format? I also need to convert 12 hour time to 24h time.
Using moment.js:
var dateInitial = "16-01-2015";
var timeInitial = "11:37 AM";
//Parse the date as String to Moment
var dateAsMoment = moment(dateInitial + " " + timeInitial, 'DD-MM-YYYY HH:mm A');
//Parse the date as Moment to String in the desired format
var dateToSend = dateAsMoment.format('YYYY-MM-DD HH:mm:ss')
http://jsfiddle.net/vcarvalho/w240pfz6/2/
// You can reverse the numbers in the date and adjust the hours as needed.
function parsedaytime(inputday, inputtime){
var dstr= inputday.value.split('-').reverse().join('-'),
tstr= inputtime.value.split(/:| /),
ampm= tstr.pop();
if(ampm== 'PM' && tstr[0]!== '12') tstr[0]-=-12;
return '"'+dstr+' '+tstr.join(':')+'"';
}
var inputday={value: "16-01-2015"},
inputtime={value: "11:37 PM"};
parsedaytime(inputday,inputtime)
returned value: (String)
"2015-01-16 23:37"
Related
I am converting DateTime to display YYYY-MM-DD hh: mm a format using moemntjs in my web application but I do not want momentjs to convert or consider timezone.
This is my date 2018-12-21T15:58:39.35-05:00 returned as part of API and need to display date without (-05:00) in YYYY-MM-DD hh: mm a format.
I am currently doing a workaround as below
var date1 = '2018-12-21T15:58:39.35-05:00'
var fmtDate = moment.utc(date1).zone("-05:00").format('YYYY-MM-DD hh:mm a');
$('#output').append(fmtDate);
Fiddle Link
Input: '2018-12-21T15:58:39.35-05:00'
Expected Output: '03:58 pm'
But I don't want to hardcode zone (-05:00) and moment.js should exclude that in formatting.
utcOffset is what you need:
var date1 = '2018-12-21T15:58:39.35-05:00'
var m = moment(date1).utcOffset(date1);
var fmtDate = m.format('YYYY-MM-DD hh:mm a');
$('#output').append(fmtDate); // result is as you expected: 2018-12-21 03:58 pm
here is the example: https://jsfiddle.net/73pa1qkz/9/
I have 2 DateTime field in a form, and I want the difference between these 2 fields in minute.
I tried to parse DateTime into Date but it's not working :
<script>
$(document).ready(function () {
$("#mybundle_evenement_button").click(function () {
var field1 = $("#mybundle_evenement_debut").val();
var field2 = $("#mybundle_evenement_fin").val();
var date1 = new Date(field1);
var date2 = new Date(field2);
alert(date1);
});
});
</script>
If I alert() date1, it shows Invalid Date.
But if I alert() field1, it shows 15/09/2017 13:32 (format is : days/months/year hour:minutes).
Is it possible that new Date(field1) isn't working because of the format ?
I know that if I succeed to parse DateTime into Date, it'll be easy to have the difference in minutes, but I don't understand why it says Invalid Date.
dd/MM/yyyy HH:mm isn't a valid date format for Date.parse()
You have to format your date to a valid Date Time String Format, for example:
var field1 = $("#mybundle_evenement_debut").val();
var ISODate1 = field1.replace(/(\d+)\/(\d+)\/(\d+)/, "$3-$2-$1")
var date1 = new Date(ISODate1);
alert(date1) // => Fri Sep 15 2017 13:32:00 ...
The problem is about the format you are getting the date from the field. new Date() don't accepts this format. I think the best is to parse the string yourself. If the format is always the same just use new Date(year, month, day, hours, minutes, seconds, milliseconds).
var day = field.splice(0,2); field.splice(0,1);
var month = field.splice(0,2); field.splice(0,1);
var year = field.splice(0,2); field.splice(0,1);
var hour = field.splice(0,2); field.splice(0,1);
var minute = field.splice(0,2);
It's depend on your browser. I'll suggest to use the standard format is '2013/12/09 10:00'.
Okay! come to the point. You need to manually format the date from my latest answer regarding this same kind of issue. Please take a look at this link : Stange javascript Date behaviour on particular dates
And you could try this below code for getting the date difference in minutes.
var startTime = new Date('2013/12/09 10:00');
var endTime = new Date('2014/12/09 10:00');
var difference = endTime.getTime() - startTime.getTime();
var result = Math.round(difference / 60000);
alert(result);
I'm pulling some data from two different APIs and I want to the objects later on.
However, I'm getting two different date formats: this format "1427457730" and this format "2015-04-10T09:12:22Z". How can I change the format of one of these so I have the same format to work with?
$.each(object, function(index) {
date = object[index].updated_at;
}
Here's one option:
var timestamp = 1427457730;
var date = new Date(timestamp * 1000); // wants milliseconds, not seconds
var dateString = date.toISOString().replace(/\.\d+Z/, 'Z'); // remove the ms
dateString will now be 2015-03-27T12:02:10Z.
Try moment.js
var timestamp = 1427457730;
var date = '2015-04-10T09:12:22Z';
var m1 = moment(timestamp);
var m2 = moment(date);
console.log(m1);
console.log(m2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
You can use .format() method in moment to parse the date to whatever format you want, just like:
m2.format('YYYY MMM DD ddd HH:mm:ss') // 2015 Apr 10 Fri 17:12:22
Check out the docs for more format tokens.
What you probably want in javascript, are date objects.
The first string is seconds since epoch, javascript needs milliseconds, so multiply it by 1000;
The second string is a valid ISO date, so if the string contains a hyphen just pass it into new Date.
var date = returned_date.indexOf('-') !== -1 ? returned_date : returned_date * 1000;
var date_object = new Date(date);
Making both types into date objects, you could even turn that into a handy function
function format_date(date) {
return new Date(date.indexOf('-') !== -1 ? date : date * 1000);
}
FIDDLE
Take a look at http://momentjs.com/. It is THE date/time formatting library for JavaScript - very simple to use, extremely flexible.
Hi i am trying to convert a json date back to a normal dd/mm/yy date.
How can i convert the Customer.DateOfBirth dd/mm/yy from a json date back to a normal date?
Here my code?
// parse the date
var Birth = Customer.DateOfBirth;
if (Birth != '') {
// get the javascript date object
Birth = DateFromString(Birth, 'dd/MM/yyyy');
if (Birth == 'Invalid Date') {
Birth = null
}
else {
// get a json date
Birth = DateToString(Birth);
//REPLACE JSON DATE HERE WITH NORMAL DATE??
}
}
Any suggestion would be great.
Thanks
If you can change the JSON representation to yyyy/mm/dd then you can directly convert it using
Birth = new Date(Birth);
If you have to use the current format, then you have to do some manual parsing
Extract the day/month/year parts and create a string in the expected format
var parts = Birth.split('/'),
day = parts[0],
month = parts[1],
year = parts[2],// you need to find a way to add the "19" or "20" at the beginning of this since the year must be full for the parser.
fixedDate = year + '/' + month + '/' + day;
Birth = new Date(fixedDate);
You can create a Date object from mm/dd/yyyy string, so if your string is ordered dd/mm/yyyy you'll either get Invalid Date error, or, possibly worse, create a Date object with wrong date (Januray 10th instead of October 1st).
So you need to swap the dd and mm parts of your string before passing it to the Date constructor:
var datestr = Customer.DateOfBirth.replace(/(\d+)\/(\d+)\/(\d+)/,"$2/$1/$3");
Birth = new Date( datestr );
You could also pass a string in yyyy/mm/dd order:
var datestr = Customer.DateOfBirth.split('/').reverse().join('/');
Birth = new Date( datestr );
I've used Date Format 1.2.3 to format my time into yyyy-mm-dd hh:MM:ss and used it for a startTime variable. How can add 4 hours to the startTime string and use it for a new variable (endTime)?
You can try to parse it as Date, add 4 hours and then format it into text again.
var d = new Date(startTime);
d.setHours(d.getHours() + 4);
var endTime = // Format d variable as you like
Parse it as a date then add 4 to the hours
Here's an example with the date object
var today = new Date();
today.setHours(today.getHours() + 4);
Javascript will automatically update the day/month/year if required