how do i get the current hebrew date

88 views
Skip to first unread message

Tal Salman

unread,
Jan 25, 2015, 8:08:44 AM1/25/15
to swift-l...@googlegroups.com
how do i get the current hebrew date and parse it to: year, month, week, day

Brent Royal-Gordon

unread,
Jan 25, 2015, 8:58:20 AM1/25/15
to Tal Salman, swift-l...@googlegroups.com
I’m not going to write your code for you, but I’ll outline some things that will help:

The Cocoa date system consists of four major classes:

• NSDate represents an instant in time, without any sort of information about how a person would identify that instant. You can compare two NSDates to see which is earlier, or get the number of seconds between two dates, or calculate a date a certain number of seconds earlier or later. But an NSDate does not have a year, month, day, hour, minute, second, time zone, calendar system, or anything of the sort. It’s just an abstract moment in time, the way “now” or “twenty minutes from now” or “when the Berlin Wall fell" is the same time whether you’re using the Gregorian, Hebrew, Islamic, Japanese, or French revolutionary calendar.

• NSDateComponents represents a date and time as humans understand it: something with years, months, etc. You can manipulate all the individual fields, but you can do so in ways that aren’t really valid (like setting the seconds field to 90), and you can’t really compare two NSDateComponents.

• NSCalendar represents a calendar system. It can convert an NSDate into the corresponding NSDateComponents, or vice versa, bridging the gap between the two worlds. By passing a different calendar identifier when you create your NSCalendar, you can get a different system; NSCalendarIdentifierHebrew is the Hebrew calendar, and depending on what you’re trying to do, you might be interested in NSCalendarIdentifierGregorian as well.

• NSDateFormatter uses all of these classes together, plus NSLocale, to convert between NSDates and Strings. You can go in either direction, and use NSDateFormatter’s options to control almost everything about how the conversion is done, including changing the calendar system.

I’m not entirely clear on what you’re trying to do, but here are a few possibilities:

• If you’re trying to take the current date and output it as a string in the Hebrew calendar, you’ll get an NSDate representing the current moment with NSDate(), then configure an NSDateFormatter with a Hebrew NSCalendar and whatever other options you want, and convert your NSDate to a String.

• If you’re trying to take the current date and break it down into Int year/month/day/etc. so you can do something else with them, you’ll get the current date with NSDate(), then get a Hebrew NSCalendar and instruct it to convert that date to an NSDateComponents, specifying the fields you’re interested in. The resulting NSDateComponents object will contain all the information you need to do whatever it is you’re trying to do.

• If you’re converting between user-provided Gregorian dates and Hebrew dates, you’ll do one of the above in reverse to get an NSDate, then do it forward again to get the NSDateComponents in the other calendar system.

Hope this helps,


Brent Royal-Gordon
Sent from Mailbox


On Sun, Jan 25, 2015 at 5:08 AM, Tal Salman <tal.s...@gmail.com> wrote:

how do i get the current hebrew date and parse it to: year, month, week, day

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/a7076a44-dad9-47fd-a0b0-af7dc1ae39fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tal Salman

unread,
Jan 25, 2015, 9:17:12 AM1/25/15
to swift-l...@googlegroups.com
thanks for your lovely answer.
i am sure it will help me.
i will read it carefully and try to do it.
thanks

Tal Salman

unread,
Mar 2, 2015, 4:10:17 AM3/2/15
to swift-l...@googlegroups.com
ok i read all about what you mention.
but i still don't know how to convert this code from objective c to swift.
i think this code will help me get the current hebrew date:

There's a handy class called NSCalendar. You create one like this:

NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendar * hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];

Once you've got the calendar objects, you can use them to convert a date around to various representations:

NSDate * date = [NSDate date];
NSDateComponents * components = [gregorian components:NSUIntegerMax fromDate:date];
NSDate * hebrewDate = [hebrew dateFromComponents:components];

NSLog(@"date: %@", date);
NSLog(@"hebrew: %@", hebrewDate);





On Sunday, January 25, 2015 at 3:08:44 PM UTC+2, Tal Salman wrote:

Marco S Hyman

unread,
Mar 2, 2015, 2:32:41 PM3/2/15
to Tal Salman, swift-l...@googlegroups.com
> but i still don't know how to convert this code from objective c to swift.

Cut and paste this into a playground. It is not necessarily a good way to
do the job but is as close to a line by line conversion as I could get.


// NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
let gregorian = NSCalendar(calendarIdentifier: NSGregorianCalendar)

// NSCalendar * hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
let hebrew = NSCalendar(calendarIdentifier: NSHebrewCalendar)

// NSDate * date = [NSDate date];
let date = NSDate()
let components = gregorian!.components(NSCalendarUnit(rawValue: UInt.max), fromDate: date)

// NSDateComponents * components = [gregorian components:NSUIntegerMax fromDate:date];

// NSDate * hebrewDate = [hebrew dateFromComponents:components];
let hebrewDate = hebrew!.dateFromComponents(components)

// NSLog(@"date: %@", date);
// NSLog(@"hebrew: %@", hebrewDate);

date // "Mar 2, 2015, 11:29 AM"
hebrewDate // "Nov 16, 1747, 11:29 AM"


If you are having troubles may I suggest you spend some more time with the
interoperability section of the Using Swift with Cocoa and Objective-C
doc.

Marc

Tal Salman

unread,
Mar 3, 2015, 3:07:43 AM3/3/15
to swift-l...@googlegroups.com
First, you are great and so helped me to understand to switch between objective c and swift from your rows.
you are right, the code i gave you not do the job i want.
so i have found how to do it:

let hebrew = NSCalendar(calendarIdentifier: NSHebrewCalendar)

    let date = NSDate()

    let formatter = NSDateFormatter()

    formatter.dateStyle = NSDateFormatterStyle.LongStyle

    formatter.timeStyle = NSDateFormatterStyle.ShortStyle

    formatter.calendar = hebrew

    println(formatter.stringFromDate(date))

this is the result i need:

12 Adar 5775 at 10:04 AM"

thanks man, very helped me




On Sunday, January 25, 2015 at 3:08:44 PM UTC+2, Tal Salman wrote:
Reply all
Reply to author
Forward
0 new messages