I want to convert todays date into the julian date format . Suppose if the date is 31/12/2011(31st dec 2011) , then the julian date should be 11365(yyddd) .
Any help would be much appreciated
Thanks
One simple way to get the date in yyddd format would be doing some mathematics:
date theDate = 31\12\2011;
int theOrdDate;
;
theOrdDate = 1000 * (year(theDate) mod 100) + dayOfYr(theDate);
// or
theOrdDate = 1000 * (year(theDate) - 2000) + dayOfYr(theDate);
EDIT: one possible drawback: this calculation will return only 4 digits if the year is less than 2010.
In AX2012 this task is super easy.
str myJulian;
myJulian = date2Julian(today());
Related
I want method to convert string 24 hours time format to 12 format
For example the method takes string 16:30
Then return string 4:30 pm
The intl package do not work because the package needs the data and time but I'm reading the time from Jason file without date
as an option
String convertTo12(String hhMM) {
final arr = hhMM.split(':');
final h = int.tryParse(arr[0]);
return '${h > 12 ? h % 12 : h}:${arr[1]}';
}
final str = '16:30';
print(convertTo12(str)); // 4:30
I'm struggling with formatting a date to insert into a calendar. I'm working on an Ionic project and I'm attempting to leverage moment.js timezones. The data is being passed into the application in several pieces. I am receiving a millisecond date time stamp, a 24-hour string for time and a string of the timezone. The date does not contain the time. What I want to achieve is creating a date object from these pieces and then converting the date into the user's local timezone to add it to their device calendar.
example of data passed to app
The date of the event: August 14, 2018 17:00
time = 17:00
date = 1534204800
timezone = AEDT
The destination timezone is based on the user's location.
let timeFormatter = new Date();
timeFormatter.setMilliseconds(date);
let momentHrMin = moment(timeFormatter.toDateString() + " " + time);
//WP sever is on GMT get the day, month and yr
let momentTZDate = momentTz.unix(date);
momentTZDate.tz('GMT');
let day = momentTZDate.days();
let month = momentTZDate.month();
let yr = momentTZDate.year();
//set the correct timezone on the ecpoh/unix DATE with momentTz.
momentTZDate.tz(this.eventDetails.eventDetail.timezoneOffset);
// Lastly set the date so the timezone conversions are correct
momentTZDate.set(
{
day: day,
month: month,
year: yr,
hour: momentHrMin.hour(),
minute: momentHrMin.minute(),
second: 0,
millisecond: 0
}
);
I found the cause of my struggles with the timezones. The abbreviated AEDT is not an accepted value for Moment.tz. AEDT could represent multiple timezones. For example, American Eastern, Australian Eastern, etc... Simply changing momentTZDate.tz("AEDT"); to momentTZDate.tz("Australia/Brisbane"); fixed my issue. Below is my typescript code I used to get through this issue. I won't say its the best but it works.
private getCalDate(date:number, time: string): Date {
// create an object in which the hours and minutes can be parse from(do to free text entry on the backend moment handles different inputs)
let timeFormatter = new Date();
timeFormatter.setMilliseconds(date);
let momentHrMin = moment(timeFormatter.toDateString() + " " + time);
//WP sever is on GMT get the day, month and yr
let momentTZDate = momentTz.unix(date);
momentTZDate.tz('GMT');
let day = momentTZDate.days();
let month = momentTZDate.month();
let yr = momentTZDate.year();
//set the correct timezone on the ecpoh/unix DATE with momentTz.
momentTZDate.tz("Australia/Brisbane");
// Lastly set the date so the timezone conversions are correct
momentTZDate.set(
{
day: day,
month: month,
year: yr,
hour: momentHrMin.hour(),
minute: momentHrMin.minute(),
second: 0,
millisecond: 0
}
);
return momentTZDate.toDate();
}
I have an NSDate from a string of 01/01/14. Unfortunately the strings are saved with two digits so I have no idea if the year is from 1900's or 2000's. But I want create some intelligent code to guess what millennium and century the full year is. I know for a fact (based on the application I'm creating) that the year can't be below 1900 in 99% of cases.
I have converted the date to now show four digits on but it's now being displayed as 01/01/0014.
Is there a way to detect the millennium of the NSDate as 0 and make my changes accordingly?
What I really want to do is something like this (pseudo code):
if (millennium == 0 && decade < 15 && decade > 99)
{
millennium = 2;
century = 0;
// change the date to 2000's here
}
else if (millennium == 00)
{
millennium = 1;
century = 9;
// change the date to 1900's here
}
For starters your pseudocode isn't quite right (the first if statement is an impossible condition as decade can't be less than 15 and more than 99 at the same time).
I have created a basic solution using NSDateComponents and made up my own conditions for when to change the year which you can easily change.
NSDate *date; // this is your date
NSDateComponents* components = [[NSCalendar currentCalendar] components: NSMonthCalendarUnit | NSDateCalendarUnit | NSYearCalendarUnit fromDate:date];
if([components year] < 15) // 2000 to 2015
{
[components setYear:[components year] + 2000];
}
else if([components year] < 100) // 1915 to 1999
{
[components setYear:[components year] + 1900];
}
date = [NSCalendar currentCalendar] dateFromComponents:components];
there is an oldschool stuff (from the 80s) here to convert a year in 2 digits to year in 4 digits.
int finalYear = (19 + (yearIn2Digits < 50)) * 100 + yearIn2Digits;
then you can use that finalYear value to build up the proper NSDate object.
the covered interval is always 100 years and it is up you where you'd like to draw the line. the original idea worked between 1950 and 2049 – as you see in the formula, – but you can refine it for covering better your actual bounds of years.
I wouldn't recommend hardcoding this logic.
what if you want to convert a date which is 1908 , or 2015.
A smarter way to do this is as follows :
-(BOOL)isInCurrentDecade:(NSDate*)date{
// I don't recollect the api to extract year from NSDate
NSInteger tmpYear = [date year];
NSInteger currentYear = [[NSDate date] year];
return tmpYear > currentYear%100
}
What I am doing here is that I compare 87 , 04 , 99 with current year digit's 14. and if it's between 00 and now then its current decade. This code is more robust than your's because it's relative comparison with current date
o/p of the code is as follow as of year 2014 :
87 - > NO // in 90's
99 -> NO
04 - > YES // in 2000's
12 - > YES
Edge case occurs when your date includes 1/1/14 if you want it to be filtered in current decade replace the '>' with ' >=" .
Use NSDateFormatter and its property "twoDigitStartDate". You set this to the earliest date that you want to be able to enter with a two digit year. No complicated logic needed, and easy to update. Now data entry is one thing, but storing a date "with a two digit year", that's criminal.
And it handles things like "I want to be able to store anything starting with the start of the tax year 1985 in two digits, that is April 1st 1985". Then April 1st 85 will be in 1985, while March 31st 85 will be in 2085. With no effort.
I'm working with the date and time in my Titanium based iOS application.
My issue is, I'm getting the time in the below format:
Wed Jan 16 2013 17:32:40 GMT+0530 (IST)
I want to change it like:
Jan 16, 2013, 05:32 pm
In iOS there is NSDateFormatter for doing this.
What I did :
Currently I'm doing it manually using split and switch cases, but it fails if the input time format changes, then I need to re-write the code for the changed format.
I searched alot in Titanium Docs, but didn't get any solution.
I asked same question on their forum, didn't get any reply till now.
Is there is anyway to do this in Titanium?
Please help, Thanks in advance.
Look for moment.js JavaScript module for date formatting
I didn't find any alternatives for the NSDateFormatter in Titanium.
I tested the Moment.js, but it didn't worked for my case.
I implemented a JavaScript function for achieving my output.
/**
* Function for formatting the date and time
* #param {Object} dateTime
*/
function formatDateTime(dateTime)
{
var arr = dateTime.split(' ');
var convertedDate = arr[1]+' '+arr[2]+', '+arr[3];
var convertedTime = arr[4];
var returnValue = convertedDate+', '+convertedTime;
return returnValue;
}
Edit on 27 Feb 2014
After facing the JavaScript date formatting issue on different occasions, I finally developed my own JavaScript module, this module will format the JavaScript Date object to Specified formats. You can find it here : MMP_DateTime
I write one function which will convert date as per my requirement as given below
var getRequiredDate = function(date) {
var monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear(); // Use standard date methods
var newdate = monthList[month] + ' ' + day + ", " + year;
Ti.API.info("Date :" + newdate);
};
Hope this helps you.
I have String time="02:30 PM" means 12 hour format and i want to convert this time in 24 hour format .
I want this o/p: 14:30. so how can i convert this ?
I don't know anything about berries, but in case the API is missing a proper formatting function, you can always get your hands dirty with the string itself:
static String convert(String time){
boolean pm = "PM".equals(time.substring(6).toUpperCase());
int h = Integer.valueOf(time.substring(0,2));
if (h!=12)
h+=pm?12:0;
else
h=pm?h:0;
return ((h<10)?"0":"")+h + ":" + time.substring(3,5);
}
Try this code.
This will give the current time in 24 Hours format.
SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
String newTime = formate.formatLocal(System.currentTimeMillis());