I try to get the correct date format, like this: 24-7-2015.
date = new Date("24-7-2015").toString({ dateFormat: 'd-M-yy' })
but the output of date is then: Wed Dec 7 00:00:00 UTC+0100 2016
Thank you
See this answer
If you want to make it easely, like in your example try to use momentjs
moment('24-7-2015', 'D-M-YYYY').format('DD-MM-YY');
Please try with the below code snippet.
var date = new Date("24-7-2015") // If this is not worked than check your local system date format
document.write(date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear());
Let me know if any concern.
Related
i am trying to convert a string into date type.i am giving the string value to new date().
but it's returning next day date instead of date which i am trying to convert.
var endDate = new Date("2017-03-23T23:59:59.000Z");
//end date value is now ------ Fri Mar 24 2017 05:29:59 GMT+0530 (India Standard Time).
Please suggest me how can get correct date in the format MM/DD/YYYY
This hack can help you,
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
it will give you,
"2017-03-23T23:59:59.000Z"
Further if you want to convert it to DD/MM/YYYY then you can use native javascript or lib like moment for that,
This simpile js will help to convert it to any format.
var endDate = new Date("2017-03-23T23:59:59.000Z").toISOString();
var d1 = endDate.split('T'); //spliting date from T
var d2 = d1[0].split('-'); //getting date part
console.log('yyyy/MM/dd', d2[0] + "/" + d2[1] + "/" + d2[2]) //YYYY/MM/DD
console.log("DD/MM/YYYY", d2[2] + "/" + d2[1] + "/" + d2[0])
jsfiddle link
if your time is in IST use below
var endDate = new Date("2017-03-23T23:59:59.00+0530");
If you check dates, you will see that your dates differs in 5h 30 mins, that is same as your date saying GMT +0530. Your original date has .000Z that is time zone of GMT +0.
Make sure you use same time zone when working with date.
Try using Date.UTC('your date')
JavaScript Date objects carry no timezone information. The only reason you saw a non-UTC date is that the browser chooses by default to display dates as local time in the console. If you don't care about the date object aligning with the exact instant in local time, you can use the following format function to turn it into MM/DD/YYYY format:
function format (date) {
var mm = ('0' + (date.getUTCMonth() + 1)).slice(-2)
var dd = ('0' + date.getUTCDate()).slice(-2)
var yyyy = date.getUTCFullYear()
return mm + '/' + dd + '/' + yyyy
}
var endDate = new Date("2017-03-23T23:59:59.000Z")
console.log(endDate.toISOString())
console.log(format(endDate))
(Credit to Ranj for posting an answer using Date#toISOString before mine.)
I have created the solution over here please find below link
https://www.w3schools.com/code/tryit.asp?filename=FD0YSGRMB59W
I am updated the value of an HTML input field with data from a query:
$("##warranty_end_date#id#").val(warranty_end_date);
But the data is store in my database as a SQL Date/Time and has an output like this: May, 08 2019 00:00:00 -0400
I would like the data to be formatted like this: 05/08/2016
How can I accomplish this?
warranty_end_date = "May, 08 2019 00:00:00 -0400";
var d = new Date(warranty_end_date);
var f = ("00" + (d.getDate()).toString()).slice(-2) + "/" + ("00" + (d.getMonth()+1).toString()).slice(-2) + "/" + (1900 + d.getYear()).toString();
$("##warranty_end_date#id#").val(f);
Maybe you could format the date in the SQL query already.
Something like:
SELECT convert(varchar, getdate(), 101)
101 = mm/dd/yyyy – 10/02/2008
var date = new Date('2010-10-11T00:00:00+05:30');
alert((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear());
You can accomplish this using the following:
var date = new Date('May, 08 2019 00:00:00 -0400');
var output = date.toLocaleFormat('%m/%d/%Y');
alert(output);
Here's a jsfiddle: https://jsfiddle.net/y4zemzqg/
Also, look over using moment.js. http://momentjs.com/ Moment.js makes formatting dates and time incredibly easy. I've used it for quite some time with no issue:
var theDate = moment('May, 08 2019 00:00:00 -0400').format('MM/DD/YYYY');
alert(theDate);
Here is a JS Fiddle using the moment.js solution: https://jsfiddle.net/v923bn5s/
I have time interval e.g. "01:30:00" as the string. Now I want to convert this string to a valid DateTime in javascript to manipulate. for example: add 1 hour.
You can use the momentjs library to do this rather easily.
var epoch = moment(str).unix();
http://momentjs.com/
Also refer
http://www.sitepoint.com/beginners-guide-to-javascript-date-and-time/
I solved my problem.
first Get current DateTime by new Date() second use .toDateString() third attach my time interval e.g. "01:30:00".
new Date().toDateString() + ' ' + "01:30:00" // Mon May 09 2016 01:30:00
Now use moment.js
var t = moment(new Date().toDateString() + ' ' + "01:30:00");
ّFinaly add for example 1 hour by moment().add()
var finalTime = t.add(60, 'minutes').format("hh:mm");
demo
var stringTimeInterval = "01:30:00";
var t = moment(new Date().toDateString() + ' ' + stringTimeInterval);
document.getElementById("demo").innerHTML = t.add(60, 'minutes').format("hh:mm:00");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<div id="demo"></div>
From Server I get Date in UTC format like ,
2016-04-13T02:37:13.211316121-04:00
When I use this to display using new Date(data.Created_at) I get 7 min time difference. Like as I am displaying my date in format {{my_date | date: 'h:mm a'}}, insted showing 12:05 PM, it dispalys 11:58 AM. So I tried this,
data.Created_at = new Date(Date.UTC(data.Created_at))
which returns null value. Is there any problem in my code? How should I get perfect date?
If you check syntax of Date.UTC,
Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]])
It expects value in different variables and not in date string. You can split it and manually parse it.
You can try something like this:
JSFiddle
var d = "2016-04-13T02:37:13.211316121-04:00";
var date_arr = d.split(/[-|T|\.|:]/);
var o = new Date(Date.UTC(date_arr[0], date_arr[1], date_arr[2], date_arr[3], date_arr[4], date_arr[5]));
console.log(date_arr, o);
Also, it gives me 8:07 AM, considering the time is 2:37 and my timezone is +5:30.
Use it like this
Date.UTC(year,month,day,hours,minutes,seconds,millisec)
The code you are using is invalid way to handle date. You can use this code
new Date('2016-04-13T02:37:13.211316121-04:00').toISOString();
var created_at = new Date(createdAt);
var created_at_date = (created_at.getUTCMonth()+1) + "/" + created_at.getUTCDate() + "/" + created_at.getUTCFullYear() + "/" + created_at.getHours() + ":"
+ created_at.getMinutes() + ":" + created_at.getSeconds();
Hope this will work for you!!!
can someone help me with a javascript regex question?
I am trying to replace all the digit dates in a string to a formatted version.
This is what I have so far
txt = txt.replace(/\d{10}/g, 'Formatted Date Here');
Is this possible? Any help is greatly appreciated! Thanks!
Try this:
str = str.replace(/\d{10}/g, function($0) {
return new Date($0*1000);
});
Date accepts a time in milliseconds. That’s why you multiply the match (passed in $0) with 1000.
If you want a different format than the default format, take a look at the methods of a Date instance. Here’s an example:
str = str.replace(/\d{10}/g, function($0) {
var d = new Date($0*1000);
return (d.getMonth() + 1) + ", " + d.getDate() + ", " + (d.getHours() % 12 || 12) + ":" + d.getMinutes() + " " + (d.getHours() < 12 ? 'AM' : 'PM');
});
The JavaScript Date.format functon Amarghosh posted here might help you.
You can use replace() with a function callback to achieve this:
var txt = "This is a test of 1234567890 and 1231231233 date conversion";
txt = txt.replace(/\d{10}/g, function(s) {
return new Date(s * 1000);
});
alert(txt);
outputs:
This is a test of Sat Feb 14 2009 07:31:30 GMT+0800 and Tue Jan 06 2009 16:40:33 GMT+0800 date conversion
You will need to adjust this to use the correct date format. Also you will need to consider the issue of time zones. The time zone on the client isn't necessarily the same as that on the server.
You might even be better off formatting the date on the server to avoid such issues.
Are you sure you want to use regex? Here is a JavaScript Date format function that you might want to check out.