This date converter function working fine on chrome but not on Firefox! any one can help figure out problem.
function converter(string) {
var d = new Date(string);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'sep', 'Oct', 'Nov', 'Dec'];
var hour = d.getHours();
return d.getFullYear() + ' ' + months[d.getMonth()] + ' ' + d.getDate() + ' | ' + (hour % 12) + ' ' + d.getMinutes() + ':' + (hour > 11 ? 'pm' : 'am');
}
alert(converter('2013-03-10 19:43:55'))
This will fix your code:
converter('2013-03-10T19:43:55')
Please note the T between the date and the time.
Edit: The datetime string should be an ISO 8601 format. Read more about it here.
Chrome is more forgiving of bad date formats, but that's technically not valid as input to Date.parse or new Date. You should make sure that it's a correctly formatted date (RFC2822 or ISO 8601), or if you want to allow more freeform input, use a library like http://www.datejs.com/
More information: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/parse
Related
I have a function that I am trying to get to format the date and time or just the date at the moment.
function(){
var d = new Date();
var n = d.getTime();
return 'VZW Dfill - ' + n;}
What I have tried
function(){
var d = new Date();
return 'VZW Dfill - ' + d;}
This returns
VZW Dfill - Thu Jan 30 2020 103924 GMT-0500 (Eastern Standard Time)
I would like the format to be 2020JAN30
I have also tried the following but this does not work
function(){
var d = new Date('YYYYMDD');
return 'VZW Dfill - ' + d;}
The above breaks the function.
Any help is appreciated.
This is actually surprisingly complex using pure JavaScript. Here's one (of many) solutions:
var now = new Date();
var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
var formattedDate = now.getFullYear() + months[now.getMonth()] + now.getDate();
alert(formattedDate);
Using your code from above, write the following function:
function(){
var d = new Date();
var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
d = d.getFullYear() + months[d.getMonth()] + d.getDate();
return 'VZW Dfill - ' + d;}
There is a pretty extensive thread about formatting JavaScript dates here. Most of them involve (common) third party packages.
You can use also locale
console.log(today.toLocaleDateString("zh-TW")); // 2020/1/30
This question already has answers here:
JavaScript convert string into Date with format (dd mmm yyyy) i.e. 01 Jun 2012
(6 answers)
Closed 8 years ago.
I have a date in the format yyyymmdd.
Ex: 20141004
What I need to do is to convert it like dd-mmm-yyyy.
Ex: 4 Oct 2014
There is a similar question here: JavaScript convert string into Date with format (dd mmm yyyy) i.e. 01 Jun 2012
The difference is that I have no "/" character to split by.
Another limitation is that I cannot use any external library, as this code is used inside another tool, so I need native JS.
I did it like this:
var date = "20141004";
var y = date.substring(0, 4);
var m = parseInt(date.substring(4, 6), 10);
var d = date.substring(6, 8);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
mm = months[m - 1];
document.write(d + " " + mm + " " + y);
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
I need your help,
how can I rework and restring a date string from yyyy-mm-ddd to dd/mm/yyyy?
Example: 2014-06-27, firstly replace the dash with a slash, then shift the order of the digits around to form 27/06/2014
I am not sure as to how to go about doing this?
Thanks
I've made a custom date string format function, you can use that.
var getDateString = function(date, format) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
getPaddedComp = function(comp) {
return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
},
formattedDate = format,
o = {
"y+": date.getFullYear(), // year
"M+": months[date.getMonth()], //month
"d+": getPaddedComp(date.getDate()), //day
"h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
"H+": getPaddedComp(date.getHours()), //hour
"m+": getPaddedComp(date.getMinutes()), //minute
"s+": getPaddedComp(date.getSeconds()), //second
"S+": getPaddedComp(date.getMilliseconds()), //millisecond,
"t+": (date.getHours() >= 12) ? 'PM' : 'AM'
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
formattedDate = formattedDate.replace(RegExp.$1, o[k]);
}
}
return formattedDate;
};
And now suppose you've :-
var date = "2014-06-27";
So to format this date you write:-
var formattedDate = getDateString(new Date(date), "d/M/y")
if you're using a string, then the string.split would be an easy way to do this.
C# code:
public void testDateTime()
{
string dashedDate = "2014-01-18"; // yyyy-mm-dd
var stringArray = dashedDate.Split('-');
string newDate = stringArray[2] + "/" + stringArray[1] + "/" + stringArray[0];
//convert to dd/mm/yyyy
Assert.AreEqual(newDate, "18/01/2014");
}
I am getting string data from a Google Calendar feed. The date is already set, by parameter, with the desired timezone.
2014-05-24T07:00:00.000-04:00
I know there are wonderful libraries like moment.js and date.js this will help format my date, but they also work with a Date object, which throws my date into a client's culture. At that point I am then juggling offsets. Would rather avoid that.
Other than a lot of conditional string manipulation, is there a simple way to do this, or I am oversimplifying (again)?
Example:
2014-05-24T07:00:00.000-04:00 to May, 24, 2014 - 7:00 AM
The following short code will parse your date, using the values present, without offsetting by the timezone:
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var res = /(\d+)\-(\d+)\-(\d+)T(\d+)\:(\d+)/
.exec('2014-05-24T07:00:00.000-04:00');
var am = (res[4] < 12);
var date = months[res[2]-1] + ', ' + res[3] + ', ' + res[1];
var time = am ? (parseInt(res[4]) + ':' + res[5] + 'AM') :
(res[4] - 12 + ':' + res[5] + 'PM');
var formatted = date + ' - ' + time;
console.log(formatted);
You can convert this string into a Date object like below,
new Date("2014-05-24T07:00:00.000-04:00")
Then you can easily convert this date object into your desired format by using any of the jQuery libraries such as jquery.globalize.js ...
Here you go:
var d = new Date('2014-05-24T07:00:00.000-04:00');
var calendar = {
months: {
full: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
days: {
full: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
short: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
}
};
console.log(calendar.months.short[d.getMonth()]+', '+d.getDate()+', '+d.getFullYear());
A simple function to reformat the string is:
function reformatDateString(s) {
s = s.match(/\d+/g);
var months = ['January','February','March','April','May','June','July',
'August','September','October','November','December'];
var ampm = s[3]<12? 'AM':'PM';
return months[--s[1]] + ', ' + +s[2] + ', ' + s[0] + ' - ' +
(s[3]%12 || 12) + ':' + s[4] + ' ' + ampm;
}
console.log(reformatDateString('2014-05-24T07:00:00.000-04:00')); // May, 24, 2014 - 7:00 AM
console.log(reformatDateString('2014-05-24T17:00:00.000-04:00')); // May, 24, 2014 - 5:00 PM
console.log(reformatDateString('2014-05-04T00:20:00.000-04:00')); // May, 4, 2014 - 12:20 AM
Which also assumes that you don't want leading zeros on single digit numbers except for the minutes, as a time like 12:5 PM isn't as readable (to me) as 12:05 PM.
Also you may need to modify the months array, it's not clear in the OP whether you want full month names or abbreviations (Jan, Feb, etc.).
I have date string (2013-03-10 19:43:55) and want to convert it in this format (Mar 10, 2013 | 7:43 pm) using JavaScript or jQuery. How can I do this ??
Extending #arun-p-johny solution for a cross-browser compatible solution
function converter(s) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
s = s.replace(/-/g, '/');
var d = new Date(s);
var hour = d.getHours();
return d.getFullYear() + ' ' + months[d.getMonth()] + ' ' + d.getDate() + ' | ' + (hour % 12) + ':' + d.getMinutes() + ' ' + (hour > 11 ? 'pm' : 'am');
}
console.log(converter('2013-03-10 19:43:55'));
Reference: http://dygraphs.com/date-formats.html
Try something like
function converter(string) {
var d = new Date(string);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'sep', 'Oct', 'Nov', 'Dec'];
var hour = d.getHours();
return d.getFullYear() + ' ' + months[d.getMonth()] + ' ' + d.getDate() + ' | ' + (hour % 12) + ' ' + d.getMinutes() + ':' + (hour > 11 ? 'pm' : 'am');
}
alert(converter('2013-03-10 19:43:55'))
Demo: Fiddle
Update:
RegEx based solution for cross browser compatibility
function converter(string) {
var d = string.split(/[-:\s]/);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'sep', 'Oct', 'Nov', 'Dec'];
var hour = parseInt(d[3], 10);
return d[0] + ' ' + months[parseInt(d[1], 10) - 1] + ' ' + d[2] + ' | ' + (hour % 12) + ' ' + d[4] + ':' + (hour > 11 ? 'pm' : 'am');
}
alert(converter('2013-03-10 19:43:55'))
Demo: Fiddle
Kashif's answer seems to be working +1, however, there is a small glitch.
If the minutes to be displayed are less than 10 then the according to his function instead of displaying "6:08 PM" it displays "6:8 PM". This can be solved by adding,
var minutes = d.getMinutes();
if(minutes<10){
minutes = "0"+minutes;
}
Complete working function will be as follows,
function dateConverter(s) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
s = s.replace(/-/g, '/');
var d = new Date(s);
var hour = d.getHours();
var minutes = d.getMinutes();
//to display the minutes less than 10 with 0 as the suffix
if(minutes<10){
minutes = "0"+minutes;
}
return months[d.getMonth()] + ' ' + d.getDate() + ' , ' + d.getFullYear() + ' | ' + (hour % 12) + ':' + minutes + ' ' + (hour > 11 ? 'pm' : 'am');
}